Php 如何在表单成功提交后重置recaptcha v2

Php 如何在表单成功提交后重置recaptcha v2,php,recaptcha,Php,Recaptcha,我按照本教程将recaptcha v2复选框添加到我的联系我们表单中。contactus表单位于contactus.php中,它调用sendmail.php发送电子邮件。按“提交”后,“联系我们”表单仍会显示在页面中,并且会弹出一条消息,表示感谢与我们联系,显示表单提交是否成功 我能够成功地将recaptcha v2添加到联系我们表单中。问题在于,由于勾选的recaptcha框在表单成功提交后仍会显示,因此在表单成功提交一分钟后会显示一条错误消息“验证已过期。请重新勾选” 这是我在contact

我按照本教程将recaptcha v2复选框添加到我的联系我们表单中。contactus表单位于contactus.php中,它调用sendmail.php发送电子邮件。按“提交”后,“联系我们”表单仍会显示在页面中,并且会弹出一条消息,表示感谢与我们联系,显示表单提交是否成功

我能够成功地将recaptcha v2添加到联系我们表单中。问题在于,由于勾选的recaptcha框在表单成功提交后仍会显示,因此在表单成功提交一分钟后会显示一条错误消息“验证已过期。请重新勾选”

这是我在contactus.php中的表单:-

<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
                        <div class="col-sm-12 ">
                            <div class="form-group">
                                <label>Full Name *</label>
                                <input type="text" name="fullname" id="fullname" class="form-control" required>
                            </div>
                            <div class="form-group">
                                <label>Email *</label>
                                <input type="email" name="email" id="email"  class="form-control" required>
                            </div>
                            <div class="form-group">
                                <label>Contact Number</label>
                                <input type="number" name="phone" class="form-control">
                            </div>
                            <div class="form-group">
                                <label>Company Name</label>
                                <input type="text" class="form-control" name="company">
                            </div>
                        </div>
                        <div class="col-sm-12">
                            <div class="form-group">
                                <label>Subject *</label>
                                <input type="text" name="subject" id="subject" class="form-control" required>
                            </div>
                            <div class="form-group">
                                <label>Message *</label>
                                <textarea name="message" id="message"  required class="form-control" rows="8"></textarea>
                            </div>
                            <div class="form-group">
                                <div class="g-recaptcha" data-sitekey="-my key-"></div>
                            </div>
                            <div class="form-group SubmitGroup">
                                <button type="submit" name="submit" class="btn btn-primary btn-lg submitcontactform" required="required">Submit Message</button>
                            </div>
                        </div>
                    </form>


全名*
电子邮件*
联系电话
公司名称
主题*
信息*
提交消息
这是我的sendmail.php:-

<?php
header('Content-type: application/json');
$status = array(
    'type'=>'success',
);

function post_captcha($user_response) {
    $fields_string = '';
    $fields = array(
        'secret' => '-my key-',
        'response' => $user_response
    );
    foreach($fields as $key=>$value)
        $fields_string .= $key . '=' . $value . '&';
    $fields_string = rtrim($fields_string, '&');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
    curl_setopt($ch, CURLOPT_POST, count($fields));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);

    $result = curl_exec($ch);
    curl_close($ch);

    return json_decode($result, true);
}

// Call the function post_captcha
$res = post_captcha($_POST['g-recaptcha-response']);

if (!$res['success']) {
    $msg = array("status" => 1, "message" => "Please do the Recaptcha.");
    echo json_encode($msg); die;
} else {
    $name = trim(stripslashes($_POST['fullname']));
    $email = trim(stripslashes($_POST['email']));
    $subject = trim(stripslashes($_POST['subject']));
    $message = trim(stripslashes($_POST['message']));
    $phone=trim(stripslashes($_POST['phone']));
    $company=trim(stripslashes($_POST['company']));
    $email_fixed = 'form@gmail.com';
    $body = 'Full Name: ' . $name . "\n\n" . 'Email Address: ' . $email . "\n\n" . 'Contact Number: ' . $phone . "\n\n" . 'Company Name: ' . $company. "\n\n" . 'Subject: ' . $subject . "\n\n\n" . 'The message is: ' . "\n" . $message;
    $headers = "From: ".$email."\r\n";
    $success = mail('test@gmail.com', 'New Message from the Contact Us Form ', $body, 'From: <'.$email_fixed.'>');
    $msg = array("status" => 1, "message" => "Thank you for your message. We will get in touch with you soon.");
    echo json_encode($msg); die;
}

注释不用于扩展讨论;这段对话已经结束。