php文件中jquery脚本中的php代码
我有一个表单php文件中jquery脚本中的php代码,php,jquery,Php,Jquery,我有一个表单filename.php,在这个表单中我调用了一个jquery函数,我想在这个jquery函数中包含以下php代码 <?php //require_once('recaptchalib.php'); $privatekey = "your_private_key"; $resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"],
filename.php
,在这个表单中我调用了一个jquery函数,我想在这个jquery函数中包含以下php代码
<?php
//require_once('recaptchalib.php');
$privatekey = "your_private_key";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
// Your code here to handle a successful verification
}
?>
现在在js代码中,如果我有下面的代码,如果我输入了正确的验证代码,那么它也会发出错误警报
var cverify = '<?php echo json_encode($cverify); ?>';
alert(cverify);
var cverify='';
警报(cverify);
那么,当单击“提交”按钮时,如何进行检查验证呢?提交时,向filename.php发送一个ajax请求,将公式中输入的验证码作为参数,并在filename.php中检查您的验证码。如果检查正确,则返回“true”作为字符串,否则返回“false” 在完成时的ajax请求中,将filename.php的返回值与“true”进行比较。如果成功,请继续向服务器提交表单 请确保您还检查了表单服务器端,因为有人可能禁用了javasctipt,因此可以提交未经检查的数据。因此,您需要再次检查服务器端的验证码。 Ajax调用:
$.ajax({
url: "filename.php",
}).done(function(jqXHR, textStatus) {
if (jqXHR.response != "correct") {
alert('wrong captcha');
} else {
alert('correct');
}
});
filename.php:
<?php
//require_once('recaptchalib.php');
$privatekey = "your_private_key";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
echo 'wrong';
} else {
// Your code here to handle a successful verification
echo 'correct';
}
/* EOF */
我觉得你的问题不太清楚。。。你能展示一下你现在有什么吗。。PHP文件中的jQuery函数。是否尝试使用PHP作为后端在javascript/jQuery中验证recaptcha?你不应该使用ajax吗?是的,你是对的。。。。。但我不知道如何在这里使用……Javascript是客户端,php是服务器端。
<?php
//require_once('recaptchalib.php');
$privatekey = "your_private_key";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
echo 'wrong';
} else {
// Your code here to handle a successful verification
echo 'correct';
}
/* EOF */