这是我的html:
<form id="form" method="post" action="php/mail.php">
<h5>Send me a message so we can get your initial consultation booked right away and get your life where you want it!</h5>
<div class="form-inline">
<p id="returnmessage"></p>
<p class="first-name-error"></p>
<label for="Firstname">Name</label>
<input type="text" class="form-control"
id="Firstname" name="first_name" placeholder="First Name">
<input type="text" class="form-control"
id="Lastname" name="last_name" placeholder="Last Name">
<p class="last-name-error"></p>
</div>
<div class="form-group">
<label for="Email">E-mail</label>
<p class="email-error"></p>
<input type="email" class="form-control"
id="Email" name="email" placeholder="Email Address">
</div>
<div class="form-group">
<label for="Subject">Subject</label>
<p class="subject-error"></p>
<input type="text" class="form-control" id="Subject" name="subject" placeholder="Let me know what it's about">
</div>
<div class="form-group">
<label for="Message">Message
</label>
<p class="message-error"></p>
<textarea class="form-control"
id="Message" name="message" rows="6" placeholder="Send me a message!"></textarea>
</div>
<input type="submit" class="btn btn-default" id="submit" name="submit" value="Submit">
</form>
这是我的用于Recaptcha的app.js:
$('#form').submit(function(event){
event.preventDefault();
var firstName = $('#Firstname').val();
var lastName = $('#Lastname').val();
var email = $('#Email').val();
var subject = $('#Subject').val();
var message = $('#Message').val();
// Validation of form field.
if (firstName == '' || lastName == '' || subject == '' || email == '' || message == '') {
event.preventDefault();
if(firstName === '' && lastName === '') {
$('.first-name-error').text('*Please enter your first and last name');
}
else if(lastName !== '' && firstName === ''){
$('.first-name-error').text('*Please enter your first name');
}
else if (firstName !== '' && lastName === ''){
$('.first-name-error').text('*Please enter your last name');
}
if (email === ''){
$('.email-error').text('*Please enter your email address');
}
if (subject === ''){
$('.subject-error').text('*Please enter a subject you want to chat about');
}
if (message === '') {
$('.message-error').text('*Please enter your message for me');
}
}
grecaptcha.ready(function() {
grecaptcha.execute('', {action: 'mail'}).then(function(token) {
$('#form').prepend('<input type="hidden" name="token" value="' + token + '">');
$('#form').prepend('<input type="hidden" name="action" value="mail">');
$('#form').unbind('submit').submit();
});;
});
});
这是我的PHP发送邮件:
<?php
define("RECAPTCHA_V3_SECRET_KEY", '');
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
$token = $_POST['token'];
$action = $_POST['action'];
// call curl to POST request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('secret' =>
RECAPTCHA_V3_SECRET_KEY, 'response' => $token)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$arrResponse = json_decode($response, true);
// verify the response
if($arrResponse["success"] == '1' && $arrResponse["action"] == $action &&
$arrResponse["score"] <= 0.5) {
// valid submission
// go ahead and do necessary stuff
if(isset($_POST['submit'])){
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$formcontent = "From: $first_name $last_name \n \n Message: $message";
$to = '';
$mailheader = "From: $email \r\n";
mail($to, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! Your message has been recieved and I will get back to you as soon as i
can!";
}
} else {
echo "Error! ";
}
?>
主要问题是必须单击两次提交按钮才能处理数据。
我正在尝试使用recaptchaV3提交联系表。该表格正在运行,但是,我必须单击两次提交按钮。一次发送输入数据,然后再次提交数据...