在 jquery 中使用 Ajax 表单执行验证码的最佳方法?

发布于 2024-07-30 07:34:35 字数 1669 浏览 1 评论 0原文

我正在使用 ajax、json 和 jquery 编写评论脚本。

除了最难的部分我已经完成了大部分 如果用户在 X 时间内发布了 X 条评论,我的 php 脚本将向下面的 javascript 代码返回一个“验证码”触发器,告诉它该用户需要输入验证码,然后我们才能将评论发布到数据库

下面你将看到我有 3 个返回值选项:成功、错误和验证码。

当返回验证码时,我想在屏幕上打开一个弹出对话框并加载我的验证码脚本,到目前为止一切正常。

现在我需要提交验证码脚本,不仅提交验证码值,还提交我的用户评论,因此我只需要使用评论值填充隐藏的表单字段。 很简单,我只是还没有做到。 现在,如果用户得到错误的验证码值,则需要重新加载屏幕,但再次弹出验证码屏幕,并且注释仍然隐藏在表单值中。 这就是棘手的部分所在。

阅读我的计划并查看下面的代码后,您认为我走在正确的轨道上吗? 有更好的方法建议吗?

<script type="text/javascript">
$.ajax({
    type: "POST",
    url: "process.php",
    data: dataString,
    dataType: "json",
    success: function(data) {
       if (data.response === 'captcha') {
            //they are mass posting so we need to give them the captcha form
            // I can send the users sanitized comment through the captcha script somehow
            // 'data.comment' is holding the comment value
            jQuery.facebox(function() {
                // This opens up a facebox dialog on the screen and popolates it with the captcha image and script
                jQuery.get('captchabox.php', function(data) {
                    jQuery.facebox( '' + data + '')
                    data.comment;
                })
            })
       } else if (data.response === 'success') {
            // success append the sanitized-comment to the page
            $('#comments').prepend(data.comment);
       } else if (data.response === 'error') {
            // just an alert for testing purposes
            alert('sorry there was an error');
       };
    }
});
</script>

更新:

经过更多思考,我似乎需要让我的验证码也使用ajax,否则将会出现页面重新加载=(

I am working on a comment script using ajax, json and jquery.

I have most of it done except the hardest part,
If a user post X ammount of comments in X ammount of time, my php script will return a "captcha" trigger to my javascript code below, telling it that this user needs to enter a captcha code before we will post there comment to the DB

Below you will see I have 3 options for a returned value: success, error, and captcha

when captcha is returned, I want to open up a popup dialog on the screen and load my captcha script, so far everything works up to this point.

Now I need to make the captcha script submit, not only the captcha value but also my users comment, so I just need to populate a hidden form field with the comment value. Easy enough I just havent done it yet. Now if a user get the wrong captcha value, it needs to reload the screen but popup the captcha screen again and still have the comment hidden in a form value. That is where the tricky part comes in.

After reading my plan and viewing my code below, do you think I am on the right track? Any suggestions on a better way?

<script type="text/javascript">
$.ajax({
    type: "POST",
    url: "process.php",
    data: dataString,
    dataType: "json",
    success: function(data) {
       if (data.response === 'captcha') {
            //they are mass posting so we need to give them the captcha form
            // I can send the users sanitized comment through the captcha script somehow
            // 'data.comment' is holding the comment value
            jQuery.facebox(function() {
                // This opens up a facebox dialog on the screen and popolates it with the captcha image and script
                jQuery.get('captchabox.php', function(data) {
                    jQuery.facebox( '' + data + '')
                    data.comment;
                })
            })
       } else if (data.response === 'success') {
            // success append the sanitized-comment to the page
            $('#comments').prepend(data.comment);
       } else if (data.response === 'error') {
            // just an alert for testing purposes
            alert('sorry there was an error');
       };
    }
});
</script>

UPDATE:

After more thought it seems I need to make my captcha use ajax as well or else there will be a page reload =(

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

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

发布评论

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

评论(1

深白境迁sunset 2024-08-06 07:34:35

我最终按照这种方式做了,效果很好。 验证码,其他人到底是如何认为相关的尚不清楚

I ended up doing it this way and it works great. Recaptcha, how the heck the others thought that was related is unknown

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