4

我正在开发社交网络上的评论系统,我正在使用 jquery,我可以毫无问题地使用 ajax 发布评论,但有时如果用户发布太多评论或出于其他原因,我需要用户提交验证码表单。

我认为最好的方法是将其添加到当前的评论发布部分,如果 php 脚本返回响应,说明我们需要做一个验证码表单,那么我想自动打开一个对话窗口屏幕,让用户填写验证码表格,然后继续并在那里发表评论。

这对我来说有点复杂,但我认为我已经完成了大部分工作,也许你可以阅读下面的评论并帮助我处理验证码部分,主要是关于如何触发对话框打开,如何传递评论值/通过验证码发短信并在成功时再次返回评论,如果用户验证码错误,那么它将重新加载验证码

$.ajax({
    type: "POST",
    url: "processing/ajax/commentprocess.php?user=",
    data: args,
    cache: false,
    success: function (resp) {
        if (resp == 'captcha') {
            //they are mass posting so we need to give them the captcha form
            // maybe we can open it in some kind of dialog like facebox
            // have to figure out how I can pass the comment and user data to the captcha script and then post it
        } else if (resp == 'error') {
            // there was some sort of error so we will just show an error message in a DIV
        } else {
            // success append the comment to the page
        };
    }
});
4

2 回答 2

5

我想我会选择使用jQuery UI 库附带的模态对话框。然后,我会将 AJAX 调用包装在一个函数中,以便递归调用它。我将创建一个 DIV (#captchaDialog) 来处理显示验证码图像和一个用于输入答案的输入 (#captchaInput)。当用户单击模态对话框上的 OK 按钮时,我将使用新的验证码响应修改原始 args 并调用该函数。由于此解决方案只是修改了原始 args 并将其重新传递到相同的 URL,因此我相信此解决方案对您有用。

一些示例代码,减去模式对话框的 div 和输入:

var postComment = function(args) {
    $.ajax({
        type: "POST",
        url: "processing/ajax/commentprocess.php?user=",
        data: args,
        cache: false,
        success: function (resp) {
            if (resp == 'captcha') {
                $("#captchaDialog").dialog({
                    bgiframe: true,
                    height: 140,
                    modal: true,
                    buttons: {
                     ok: function() {
                        args.captchaResponse = $(this).find("#captchaInput").val();
                        postComment(args);
                     }
                    }
                });
            } else if (resp == 'error') {
                // there was some sort of error so we will just show an error message in a DIV
            } else {
                // success append the comment to the page
            };
        }
    });
};

希望这可以帮助!

于 2009-08-11T01:17:06.827 回答
0

一边想:

为了获得最佳用户体验,我会高度考虑实​​施反向验证码:

http://www.ccs.uottawa.ca/webmaster/reverse-captcha.html

我知道这不能解决您的问题的需要,但我至少不得不提到这一点。这是一种垃圾邮件预防方法,不需要代表您的用户进行任何输入。

于 2009-08-11T02:25:45.690 回答