Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 同一页上的Recaptcha警报不正确_Javascript_Php_Recaptcha - Fatal编程技术网

Javascript 同一页上的Recaptcha警报不正确

Javascript 同一页上的Recaptcha警报不正确,javascript,php,recaptcha,Javascript,Php,Recaptcha,我有密码 <form name="input" action="messagesave.php" method="POST"> <table style="margin-left: auto; margin-right: auto;"> <tr> <td style="font-family:'Comic Sans MS', cursive; font-size:20px; text-shadow: 0 0

我有密码

<form name="input" action="messagesave.php" method="POST">
    <table style="margin-left: auto; margin-right: auto;">
        <tr>
            <td style="font-family:'Comic Sans MS', cursive; font-size:20px; text-shadow: 0 0 10px #FFFFFF;">Subject:</td>
        </tr>
        <tr>
            <td><input type="text" value="(Optional)" name="sub" onblur="this.value=!this.value?'(Optional)':this.value;" onfocus="this.select()" onclick="this.value='';"></td>
        </tr>
        <tr>
            <td style="font-family:'Comic Sans MS', cursive; font-size:20px; text-shadow: 0 0 10px #FFFFFF;">Message (Required):</td>
        </tr>
        <tr>
            <td><textarea name="comment" id="comment" cols="60" rows="6"></textarea></td>
        </tr>
        <tr>
            <td>
            <?php
                require_once('recaptchalib.php');
                $publickey = "6LeSzekSAAAAAL32gFlK_vGcqI3-kuoz990KSJHU"; // you got this from the signup page
                echo recaptcha_get_html($publickey);
            ?>
            </td>
        </tr>
        <tr>
            <td><input type="submit" class="submit" value="Submit Message"></td>
        </tr>
    </table>
</form>
当用户错误地输入reCAPTCHA代码时,页面将移动到一个空白页面上,表示未正确输入reCAPTCHA

我想问,我如何实现javascript警报或出现在主页上的红色文本,这样用户就不必一直按后退按钮


谢谢

您必须将表单发送到同一个PHP脚本本身,并使用一个参数指示表单已发送(
send
在本例中):


在与表单相同的PHP脚本开头,检查表单是否已发送,输入的数据是否正确:

<?php

$errorArr = array();

if(isset($_GET['send'])) {
    // form is sent, do your captcha and other checks here and save the errors in an array
    if($captcha->wrong())
        $errorArr[] = 'You entered the captcha wrong';

   if(count($errorArr) <= 0) {
       // No errors occured so do stuff with the correct input of the user
       // save it to db for example
   }
}

?>

然后,您可以在页面的某个位置打印错误消息,如

<?php echo (count($errorArr) > 0)?implode('<br>', $errorArr):null; ?>

或者,您可以使用两个不同的页面和会话变量来完成整个过程,但这并不复杂,也不必要

如果使用Ajax检查验证码,则在发送表单时仍然需要在服务器端再次检查验证码,因为有人可以禁用javascript(因为大多数spambot无法解释javascript),而验证码验证失败

在你的情况下,你最终会得到

<?php

require_once('recaptchalib.php');

$errorArr = array();
$privatekey = "6LeSzekSAAAAAAdAxcsVugyScb8B1D6UoKpjrX2W";
$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
    $errorArr[] = 'The reCAPTCHA wasn\'t entered correctly. Go back and try it again. (reCAPTCHA said: ' . $resp->error . ')';
} 

if(count($errorArr) <= 0) {
    $comment = $_POST['comment'];
    $to = "jsmith@example.com";
    $subject = $_POST['sub'];
    $message = $comment;
    $from = "jsmith@example.net";
    $headers = "From:" . $from;

    if(mail($to,$subject,$message,$headers) === false)
        $errorArr[] = 'Mail could not be sent to us due to a technical error';

    // if headers are sent after output already sent you get an error
    //echo "Mail Sent."; 
    header( 'Location: success.html' ) ;
}

?>
<form name="input" action="messagesave.php" method="POST">
    <?php echo (count($errorArr) > 0)?implode('<br>', $errorArr):null; ?>
    <table style="margin-left: auto; margin-right: auto;">
        <tr>
            <td style="font-family:'Comic Sans MS', cursive; font-size:20px; text-shadow: 0 0 10px #FFFFFF;">Subject:</td>
        </tr>
        <tr>
            <td><input type="text" value="(Optional)" name="sub" onblur="this.value=!this.value?'(Optional)':this.value;" onfocus="this.select()" onclick="this.value='';"></td>
        </tr>
        <tr>
            <td style="font-family:'Comic Sans MS', cursive; font-size:20px; text-shadow: 0 0 10px #FFFFFF;">Message (Required):</td>
        </tr>
        <tr>
            <td><textarea name="comment" id="comment" cols="60" rows="6"></textarea></td>
        </tr>
        <tr>
            <td>
            <?php
                require_once('recaptchalib.php');
                $publickey = "6LeSzekSAAAAAL32gFlK_vGcqI3-kuoz990KSJHU"; // you got this from the signup page
                echo recaptcha_get_html($publickey);
            ?>
            </td>
        </tr>
        <tr>
            <td><input type="submit" class="submit" value="Submit Message"></td>
        </tr>
    </table>
</form>

主题:
信息(必需):

它将转到不同的页面,因为我猜您正在发布表单。您可以做的是将“提交”按钮更改为“简单”按钮。然后

 $( "#button" ).click(function() {
     //your ajax call to validating php
//on success $( "#formId" ).submit();
    });
如果不想使用javascript,只需发布表单,就可以使用变量集将表单从验证php重定向到同一页面

header("Location:http://localhost/login.php?x=1")
查证

if(isset($_GET('x'))){
//your html for error message
}

也许可以帮助

将您的表单和操作处理代码组合成一个脚本,并将其发布到自身,一般表单为:

if ($POST['submit'] && $resp->is_valid) {
    // Form sent and captcha ok

} else {
    // Check, was it submitted already?
    if (isset($resp) && !$resp->is_valid) {
        echo "<div><p>The reCAPTCHA wasn't entered correctly. Go back and try it again." . "(reCAPTCHA said: " . $resp->error . ")</p></div>"; 
    }
    // All the form code here.

}
if($POST['submit']&&$resp->有效){
//表单已发送,验证码正确
}否则{
//检查,已经提交了吗?
如果(设置($resp)&&!$resp->有效){
echo“没有正确输入reCAPTCHA。请返回并重试。”(reCAPTCHA说:“$resp->错误。”)

”; } //所有的表单代码都在这里。 }
感谢david的编辑,在stackoverflow中编写代码太难了
if(isset($_GET('x'))){
//your html for error message
}
if ($POST['submit'] && $resp->is_valid) {
    // Form sent and captcha ok

} else {
    // Check, was it submitted already?
    if (isset($resp) && !$resp->is_valid) {
        echo "<div><p>The reCAPTCHA wasn't entered correctly. Go back and try it again." . "(reCAPTCHA said: " . $resp->error . ")</p></div>"; 
    }
    // All the form code here.

}