带有 AJAX 和 Recaptcha 的 PHP 联系表检查了重新验证码时出错

问题描述

对编码相当陌生,因此大部分情况下都使用模板。几天来,我一直在抨击代码并尝试不同的事情,试图让我的联系表格与我的 js/ajax、PHP 和 recaptcha 一起使用。我尝试了不同的方法,比如完全改变代码并让它工作,但我想让我开始使用的代码工作。我正确输入了recaptcha密钥,并且表单确实通过回显数字并在页面上发布信息来让用户知道是否缺少字段,但是当recaptcha被检查和验证时,它仍然说要检查recaptcha。这个新手感谢提供的所有帮助。这是我所拥有的:

HTML

<form method="post" id="contact_form" action="contact.PHP">
     <input type="text" name="name" id="contact_name" placeholder="Your Name" class="wow fadeInRight">
     <input type="text" name="email" id="contact_email" placeholder="Email" class="wow fadeInRight">
     <input type="text" name="phone" id="contact_phone" placeholder="Phone" class="wow fadeInRight">
     <textarea name="message" id="contact_text" placeholder="Your Message" class="wow fadeInRight"></textarea>
     <br>
     <div class="g-recaptcha" data-sitekey="my public key"></div>
     <br>
     <div id="formresult"> 
        <button type="submit" name="submit" class="bbtn btn-primary wow fadeInRight">Submit</button>
         <button type="reset" name="resetbtn" class="bbtn btn-primary wow fadeInRight"> Reset </button>
     </div>
</form>

PHP

<?PHP
// Email Setting
//=======================================
$admin_email = "my email";
$from_name   = "from-info";


if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
    $user_name      = strip_tags($_POST['username']);
    $user_email     = strip_tags($_POST['useremail']);
    $user_phone     = strip_tags($_POST['userphone']);
    $comment_text   = strip_tags($_POST['commenttext']);

    if (isset($_POST['g-recaptcha-response'])) {
        $captcha = $_POST['g-recaptcha-response'];
    }

        
    if (!filter_var($user_name)) {
        echo 5;
        exit;
    } elseif (!filter_var($user_email,FILTER_VALIDATE_EMAIL)) {
        echo 6;
        exit;
    } elseif (!filter_var($user_phone)) {
        echo 7;
        exit;
    } elseif (!filter_var($comment_text)) {
        echo 8;
        exit;
    } elseif (empty($captcha)) {
        echo 9;
        exit;
    } else {
        $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret='my_secret_key'&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
        $decoded_response = json_decode($response,true);

        if ($decoded_response['success'] == true)   {
            $to             = "$admin_email"; 
            $subject        = "New Contact information";
            $message        = "Name: $user_name <br/>";
            $message        .= "Email: $user_email <br/>";
            $message        .= "Phone: $user_phone <br/>";
            $message        .= "Comment: $comment_text <br/>";
            $headers        = "MIME-Version: 1.0\r\n";
            $headers        .= "Content-type: text/html; charset=iso-8859-1\r\n";
            $headers        .= "From:$from_name<$admin_email>";
            $headers        .= "Reply-To: $admin_email\r\n"."X-Mailer: PHP/".PHPversion();
            $send           = mail($to,$subject,$message,$headers);
            echo 1;
            exit;
        }
    }
}

script.js

$("#contact_form").on("submit",function (e) {
            e.preventDefault();
            $('#show_contact_msg').html('<div class=gen>Sending Message..</div>');
            var username = $('#contact_name').val();
            var useremail = $('#contact_email').val();
            var userphone = $('#contact_phone').val();
            var commenttext = $('#contact_text').val();
            var formURL = "contact.PHP";
            var data = {
                username: username,useremail: useremail,userphone: userphone,commenttext: commenttext,captcha: grecaptcha.getResponse()
                };
            $.ajax(
                {
                    url: formURL,type: "POST",data: data,success: function (res) {
                        if (res == '1') {
                            $('#show_contact_msg').html('<div class=gen><i class="fa fa-smile-o" aria-hidden="true"></i> Thank you very much,We will notify you when we lunch</div>');
                            $("#contact_form")[0].reset();
                        }
                        if (res == '5') {
                            $('#show_contact_msg').html('<div class=err><i class="fa fa-frown-o" aria-hidden="true"></i> Please enter your Name so I kNow who you are.</div>');
                        }
                        if (res == '6') {
                            $('#show_contact_msg').html('<div class=err><i class="fa fa-frown-o" aria-hidden="true"></i> Please enter a valid email so I can contact you.</div>');
                        }
                        if (res == '7') {
                            $('#show_contact_msg').html('<div class=err><i class="fa fa-frown-o" aria-hidden="true"></i> Please enter a valid phone number so I can contact you.</div>');
                        }
                        if (res == '8') {
                            $('#show_contact_msg').html('<div class=err><i class="fa fa-frown-o" aria-hidden="true"></i> Please enter your message so I can respond to your question/request.</div>');
                        }
                        if (res == '9') {
                            $('#show_contact_msg').html('<div class=err><i class="fa fa-frown-o" aria-hidden="true"></i> Please complete the Recaptcha.</div>');
                        }
                        if (res == '10') {
                            $('#show_contact_msg').html('<div class=err><i class="fa fa-frown-o" aria-hidden="true"></i> Something went wrong.  Please try again.</div>');
                        }
                    }
                });            
        });

Picture showing recaptcha checked and error message after submit

解决方法

Please correct following line,Enter "site-key",which you can get from google-recaptcha account and try again. 
<div class="g-recaptcha" data-sitekey="my public key"></div>

Currently,as per my assumption you get ajax response as 9 for g-recaptch because google-recaptcha-response you got empty. It may be due to invalid site-key identify by google.

if (isset($_POST['g-recaptcha-response'])) {
        $captcha = $_POST['g-recaptcha-response'];
}