PHP Ajax表单,reCaptcha没有提交

我有ajax和reCaptchavalidation的表单。 我在制作reCaptcha时遇到了一些麻烦。 我一直收到来自sendmail.php的错误消息"Please click the reCAPTCHA box" (错误400)。 我在这里想念的是什么?

HTML:

   

JS:

  $(function() { // Get the form. var form = $('#contactform'); // Get the messages div. var formMessages = $('.messages'); // Set up an event listener for the contact form. $('#buttonid').click(function(e) { // Stop the browser from submitting the form. e.preventDefault(); // Serialize the form data. var formData = $(form).serialize(); // Submit the form using AJAX. $.ajax({ type: 'POST', url: $(form).attr('action'), data: formData }) .done(function(response) { // Make sure that the formMessages div has the 'success' class. $(formMessages).removeClass('error'); $(formMessages).addClass('success'); // Set the message text. $(formMessages).text(response); // Clear the form. $('#inputName').val(''); $('#inputMail').val(''); $('#inputMessage').val(''); }) .fail(function(data) { // Make sure that the formMessages div has the 'error' class. $(formMessages).removeClass('success'); $(formMessages).addClass('error'); // Set the message text. if (data.responseText !== '') { $(formMessages).text(data.responseText); } else { $(formMessages).text('Oops! An error occured, and your message could not be sent.'); } }); }); });  

sendmail.php:

 success == true) { $name = strip_tags(trim($_POST["name"])); $name = str_replace(array("\r","\n"),array(" "," "),$name); $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL); $message = trim($_POST["message"]); if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) { http_response_code(400); echo "Oops! There was a problem with your submission. Please complete the form and try again."; exit; } $recipient = "will@additlater.com"; $subject = "New message from $name"; $email_content = "Name: $name\n"; $email_content .= "Email: $email\n\n"; $email_content .= "Message:\n$message\n"; $email_headers = "From: $name "; if (mail($recipient, $subject, $email_content, $email_headers)) { http_response_code(200); echo "Thank You! Your message has been sent."; } else { http_response_code(500); echo "Oops! Something went wrong, and we couldn't send your message. Check your email address."; } } // If the Google Recaptcha check was not successful else { http_response_code(400); echo "Robot verification failed. Please try again."; } } // If the Google Recaptcha box was not clicked else { http_response_code(400); echo "Please click the reCAPTCHA box."; } } // If the form was not submitted // Not a POST request, set a 403 (forbidden) response code. else { http_response_code(403); echo "There was a problem with your submission, please try again."; } ?> 

我解决了 似乎有一些错误。

  1. 正如KyleS所提到的,reCapthca框需要在

  2. 表单输入字段需要具有name=""才能使.serialize()起作用。

之后,代码现在按预期工作。