【问题标题】:Best way to do captcha with an Ajax form in jquery?在 jquery 中使用 Ajax 表单进行验证的最佳方法?
【发布时间】:2026-01-12 12:00:01
【问题描述】:

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

除了最难的部分,我已经完成了大部分工作, 如果用户在 X 时间内发布 X 数量的 cmets,我的 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,否则会重新加载页面 =(

【问题讨论】:

标签: jquery ajax captcha


【解决方案1】:

我最终这样做了,效果很好。 Recaptcha,其他人认为这是如何相关的还不得而知

【讨论】:

    最近更新 更多