我正在尝试使用 jquery Real Person ( http://keith-wood.name/realPerson.html )。按照说明,这是我的 javascript 代码。$("#sb1") 是提交按钮。
我遇到了时间问题,因为涉及到一个 ajax 调用,其中显示了注释“//bValid 此时未定义”,bValid 未定义。有什么想法可以延迟“bValid = bValid && checkCaptcha();” 代码以便 ajax 调用到那时已经完成?
谢谢,火箭
function checkCaptcha() {
// Call php file to check this
$.post("ajax/check_captcha.php", { realPerson : $("#defaultReal").val(), realPersonHash: $(".realperson-hash").val()} ,
function(data) {
// If data = Error reload page
if ($.trim(data) == "Error") {
updateTips("Captcha does not match");
return false;
} else {
return true;
}
});
}
...
$("#sb1").click(function() {
var bValid = true;
bValid = bValid && checkLength($("#title"), "name", 3, 100 );
bValid = bValid && checkCaptcha(); //bValid is UNDEFINED at this point
alert("bValid is "+bValid);
if ( bValid ) {
setTimeout("$('#submitCampForm').submit();",500);
}
});
PHP 代码未更改 (check_captcha.php)。
function rpHash($value) {
$hash = 5381;
$value = strtoupper($value);
for($i = 0; $i < strlen($value); $i++) {
$hash = (leftShift32($hash, 5) + $hash) + ord(substr($value, $i));
}
return $hash;
}
// Perform a 32bit left shift
function leftShift32($number, $steps) {
// convert to binary (string)
$binary = decbin($number);
// left-pad with 0's if necessary
$binary = str_pad($binary, 32, "0", STR_PAD_LEFT);
// left shift manually
$binary = $binary.str_repeat("0", $steps);
// get the last 32 bits
$binary = substr($binary, strlen($binary) - 32);
// if it's a positive number return it
// otherwise return the 2's complement
return ($binary{0} == "0" ? bindec($binary) :
-(pow(2, 31) - bindec(substr($binary, 1))));
}
if (rpHash($_POST['realPerson']) == $_POST['realPersonHash']) {
} else {
print "Error";
}