我正在按照本教程https://www.youtube.com/watch?v=rFccSL76SeY的要求将Recaptcha v2复选框添加到“与我们联系”表单中。 “与我们联系”表单位于contactus.php中,它调用sendemail.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>
这是我的sendemail.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;
}
在弹出窗口中显示谢谢消息和recaptcha消息
成功提交表格后,如何重新设置验证码?某些网站在提交表单后会隐藏“与我们联系”表单,因此隐藏密码。我没有这样做,并且表格和Recaptcha仍然显示。这就是为什么我要在提交表单后重新设置验证码