jQuery ajax 验证验证码

发布于 2024-11-03 03:14:08 字数 484 浏览 1 评论 0原文

我在发布验证码以通过 php 进行验证时遇到问题。 我将 captcha_value 字符串发送到 captcha_check.php,但我不知道如何检索返回值“true”或“false”

$("#myForm").submit(function() {
$.ajax({
       type: "POST",
       url: '/captcha_check.php',
       data: captcha_value
       success: function(data) {
          **?WHAT TO DO HERE? how to get true or false**
       }
});

captcha_check.php
<?php   

if ($_POST['captcha'] == $_SESSION['captcha'])
echo 'true';
else
echo 'false';
?>

i have a problem when posting a captcha to validate by php.
I send the captcha_value string to the captcha_check.php and I don't know how to retrieve the returned value 'true' or 'false'

$("#myForm").submit(function() {
$.ajax({
       type: "POST",
       url: '/captcha_check.php',
       data: captcha_value
       success: function(data) {
          **?WHAT TO DO HERE? how to get true or false**
       }
});

captcha_check.php
<?php   

if ($_POST['captcha'] == $_SESSION['captcha'])
echo 'true';
else
echo 'false';
?>

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

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

发布评论

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

评论(3

初心 2024-11-10 03:14:08

我将标头设置为输出为 xml。

captcha_check.php

<?php   
header('Content-Type:text/xml');//needed to output as xml(that is my choice)
echo "<root><message>";
if ($_POST['captcha'] == $_SESSION['captcha'])
echo 'true';
else
echo 'false';
echo "</message></root>";
?>

$("#myForm").submit(function() {
$.ajax({
       type: "POST",
       url: '/captcha_check.php',
       dataType:'xml', 
       data: captcha_value
       success: function(data) {
          if($(data).find('message').text() == "true"){
             //now you get the true. do whatever you want. even call a function
            }
          else{
        //and false
          }
       }
});

这是我的解决方案,可能也适合您。我一直更喜欢使用 xml 进行通信。这就是我的选择。

I set header to output as xml.

captcha_check.php

<?php   
header('Content-Type:text/xml');//needed to output as xml(that is my choice)
echo "<root><message>";
if ($_POST['captcha'] == $_SESSION['captcha'])
echo 'true';
else
echo 'false';
echo "</message></root>";
?>

$("#myForm").submit(function() {
$.ajax({
       type: "POST",
       url: '/captcha_check.php',
       dataType:'xml', 
       data: captcha_value
       success: function(data) {
          if($(data).find('message').text() == "true"){
             //now you get the true. do whatever you want. even call a function
            }
          else{
        //and false
          }
       }
});

this is my solution probably works for you also. I always prefer xml for communication. Thats my choice.

绝情姑娘 2024-11-10 03:14:08
$.ajax({
    type: "POST",
    url: '/captcha_check.php',
    data: captcha_value,
    dataType: "text",
    success: function(data) {
        if(data == "true") {
            // correct
        } else {
            // nope
        }
    }
});
$.ajax({
    type: "POST",
    url: '/captcha_check.php',
    data: captcha_value,
    dataType: "text",
    success: function(data) {
        if(data == "true") {
            // correct
        } else {
            // nope
        }
    }
});
腹黑女流氓 2024-11-10 03:14:08
dataType: 'json', //Important:Sometimes JQuery fails to automatically detect it for you.
success: function(data) {
    console.log(data ? "Data is true" : "Data is false");
}
dataType: 'json', //Important:Sometimes JQuery fails to automatically detect it for you.
success: function(data) {
    console.log(data ? "Data is true" : "Data is false");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文