且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery的AJAX验证验证码

更新时间:2022-10-16 23:07:58

我设置标题为XML输出​​。

captcha_check.php

 < PHP
标题(内容类型:text / xml的'); //需要为XML输出​​(这是我的选择)
回声<根><消息>中;
如果($ _ POST ['验证码'] = = $ _SESSION ['验证码'])
回声真;
其他
回声假;
回声< /消息>< /根>中;
?>

$(#myForm会)。递交(函数(){
$阿贾克斯({
       键入:POST,
       网址:/captcha_check.php,
       数据类型:XML,
       数据:captcha_value
       成功:功能(数据){
          如果($(数据).find(信息)。文字()==真){
             //现在你得到真正的。做你想做的。甚至调用一个函数
            }
          其他{
        //假
          }
       }
});
 

这是我的解决方案可能是为你的作品也。我总是preFER XML进行通信。这就是我的选择。

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';
?>

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.