如何处理jquery ajax注释和验证码

发布于 2024-08-01 15:54:59 字数 997 浏览 3 评论 0原文

我正在社交网络上开发评论系统,我正在使用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
        };
    }
});

I am working on a comment system on a social network, I am using jquery, I can post the comments with ajax with no problem but sometimes I need a user to submit a captcha form if they are posting too many comments or for other reasons.

I think the best way to do this is to add it into the current comment posting part, if the php script returns a response, stating that we need to do a captcha form, then I would like to auto open up a dialog window on the screen, let the user fill in the captcha form, then carry on and post there comment.

This is somewhat complex for me but I have most of it done I think, maybe you can read my comments below and help me with the captcha part, mainly on how I can trigger a dialog to open, how I can pass the comment value/text through the captcha and back to the comment backen again on sucess and also if the user gets the captcha wrong, then it will reload the captcha

$.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
        };
    }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏了南城 2024-08-08 15:54:59

我想我会选择使用jQuery UI 库附带的模式对话框。 然后,我将 AJAX 调用包装在一个函数中,这样我就可以递归地调用它。 我将创建一个 DIV (#captchaDialog) 来处理显示验证码图像和一个用于输入答案的输入 (#captchaInput)。 当用户单击模式对话框上的“确定”按钮时,我将使用新的验证码响应修改原始参数并调用该函数。 由于此解决方案只是修改原始参数并将其重新传递到相同的 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
            };
        }
    });
};

希望这有帮助!

I think I would opt for using the modal dialog that comes with the jQuery UI library. Then, I would wrap the AJAX call in a function so I could recursively call it. I would create a DIV (#captchaDialog) that handled displaying the Captcha Image and an Input (#captchaInput) for entering the answer. When the user click the OK button on the modal dialog, I would modify the original args with the new captcha response and recall the function. Since this solution simply modifies the original args and repasses it to the same URL, I believe this solution will work for you.

Some sample code, minus the div and input for the modal dialog:

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
            };
        }
    });
};

Hope this helps!

傲世九天 2024-08-08 15:54:59

侧面想法:

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

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

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

Side thought:

For best user experience I would highly consider implementing reverse CAPTCHA:

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

I understand this doesn't address the needs of your question, but I had to at least mention this. It's a spam prevention method that doesn't require any input on behalf of your users.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文