jQuery的AJAX验证验证码 [英] jQuery ajax validate captcha
本文介绍了jQuery的AJAX验证验证码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题发布一个验证码由PHP来验证时。
我送了 captcha_value
字符串的captcha_check.php,我不知道该如何检索返回值真或假
$(#myForm会)。递交(函数(){
$阿贾克斯({
键入:POST,
网址:/captcha_check.php,
数据:captcha_value
成功:功能(数据){
**?在这里做什么?如何获得真或假**
}
});
captcha_check.php
< PHP
如果($ _ POST ['验证码'] = = $ _SESSION ['验证码'])
回声真;
其他
回声假;
?>
解决方案
我设置标题为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.
这篇关于jQuery的AJAX验证验证码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文