无法将reCaptcha集成到使用AJAX提交的PHP联系人表单中

无法将reCaptcha集成到使用AJAX提交的PHP联系人表单中,php,ajax,recaptcha,Php,Ajax,Recaptcha,我最近在一个已经集成了reCaptcha的工作联系人表单中添加了AJAX,因此可以在不加载新页面的情况下提交表单,并且我在获取g-reCaptcha-response时遇到了问题。我让reCaptcha PHP代码与它工作时的代码相同,我不确定需要修改或添加什么才能让它重新工作 表格: PHP文件: <?php //reCAPTCHA $public_key = "#"; $private_key = "#"; $url = "https://www.google.com/

我最近在一个已经集成了reCaptcha的工作联系人表单中添加了AJAX,因此可以在不加载新页面的情况下提交表单,并且我在获取g-reCaptcha-response时遇到了问题。我让reCaptcha PHP代码与它工作时的代码相同,我不确定需要修改或添加什么才能让它重新工作

表格:

PHP文件:

<?php
  //reCAPTCHA
  $public_key = "#";
  $private_key = "#";
  $url = "https://www.google.com/recaptcha/api/siteverify";

  $response_key = $_POST['g-recaptcha-response'];
  $response = file_get_contents($url.'?secret='.$private_key.'&response='.$response_key.'&remoteip='.$_SERVER['REMOTE_ADDRESS']);
  $response = json_decode($response);

  //For trouble shoooting
  // echo "<pre>";print_r($_POST);echo "</pre>";
  // echo "<pre>";print_r($response);echo "</pre>";

  if($response->success == 1) {
    $name = $_POST['name'];
    $company = $_POST['company'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $formcontent=" From: $name \n Company: $company \n Phone: $phone \n Email: $email \n Message: $message";
    $recipient = "justin.toland@powereng.com, jason.pfaff@powereng.com";
    $subject = "Geovoice.io Inquiry";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
  }     
?>
目前,在我点击提交按钮后,弹出警报显示的第一件事是未定义的索引:g-recaptcha-response。在本例中,我没有输入reCaptcha键。

您没有在POST调用中传递g-reCaptcha-response参数,因此:

应该是


我们又开始做生意了,谢谢!我应该注意到这一点。我收到另一个警报错误,原因是$_服务器['REMOTE_ADDRESS']和远程IP地址也没有通过。我删除了那个部分,因为我读到它是可选的,一切正常。
 var submit = document.getElementById('submit');
 var form = document.forms["contact-form"];

 function submitFormAjax() {
   var name = form['name'].value;
   var company = form['company'].value;
   var phone = form['phone'].value;
   var email = form['email'].value;
   var message = form['message'].value;
   var xmlhttp= window.XMLHttpRequest ?
   new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");

   xmlhttp.onreadystatechange = function() {
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
       alert(xmlhttp.responseText);
     }
   }
   xmlhttp.open("POST","mail.php",true);
   xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
   xmlhttp.send("name=" + name + "&company=" + company + "&phone=" + phone + "&email=" + email + "&message=" + message);
  }

 submit.onclick = function(e) {
   if (grecaptcha.getResponse() == ""){
     alert("Please click the reCAPTCHA checkbox!");
     return false;
   } else {
     submitFormAjax();
   }
 }
<?php
  //reCAPTCHA
  $public_key = "#";
  $private_key = "#";
  $url = "https://www.google.com/recaptcha/api/siteverify";

  $response_key = $_POST['g-recaptcha-response'];
  $response = file_get_contents($url.'?secret='.$private_key.'&response='.$response_key.'&remoteip='.$_SERVER['REMOTE_ADDRESS']);
  $response = json_decode($response);

  //For trouble shoooting
  // echo "<pre>";print_r($_POST);echo "</pre>";
  // echo "<pre>";print_r($response);echo "</pre>";

  if($response->success == 1) {
    $name = $_POST['name'];
    $company = $_POST['company'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $formcontent=" From: $name \n Company: $company \n Phone: $phone \n Email: $email \n Message: $message";
    $recipient = "justin.toland@powereng.com, jason.pfaff@powereng.com";
    $subject = "Geovoice.io Inquiry";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
  }     
?>
xmlhttp.send("name=" + name + "&company=" + company + "&phone=" + phone + "&email=" + email + "&message=" + message);
xmlhttp.send("name=" + name + "&company=" + company + "&phone=" + phone + "&email=" + email + "&message=" + message + "&g-recaptcha-response=" + grecaptcha.getResponse());