Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从javascript/Google recaptcha调用php文件_Javascript_Php - Fatal编程技术网

从javascript/Google recaptcha调用php文件

从javascript/Google recaptcha调用php文件,javascript,php,Javascript,Php,如果我的问题的答案是显而易见的,很多人表示歉意 我正在尝试将Google reCaptcha整合到我的网站中 这是我页面头部的javascript: var onSubmit = function(token) { $.ajax({ type: 'POST', url: 'assets/php/contact.php', success: function(){ alert('Thank you for your request '

如果我的问题的答案是显而易见的,很多人表示歉意

我正在尝试将Google reCaptcha整合到我的网站中

这是我页面头部的javascript:

  var onSubmit = function(token) {
    $.ajax({
      type: 'POST',
      url: 'assets/php/contact.php',
      success: function(){
        alert('Thank you for your request ' + document.getElementById('name').value);
      }
    });
  };
reCaptcha是可见的,并且正在工作

上面的代码给出了“
成功:
”的结果,但没有使用对php文件的调用来处理表单

页面上的所有其他脚本都工作正常

下面是contact.php代码:

我已经对代码做了这些更改。然而,结果没有变化。以下是php文件的代码:

<?php
    //contact form submission code

  //Validate data on the form 
  // define variables and set to empty values
  $name = $email = $message = "";

  if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = contact_input($_POST["name"]);
    $email = contact_input($_POST["email"]);
    $message = contact_input($_POST["message"]);
  }

  function contact_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
  }
  // if the url field is empty
if(isset($_POST['url']) && $_POST['url'] == ''){
  // put your email address here
  $youremail = 'my@email.co.uk';
  // prepare a "pretty" version of the message
  // Important: if you added any form fields to the HTML, you will need to add them here also
  $body = "Contact Form from Wilderness Canoe website just submitted:
  Name: $_POST[name]
  E-Mail: $_POST[email]
  Message: $_POST[message]";
  // Use the submitters email if they supplied one
  // (and it isn't trying to hack your form).
  // Otherwise send from your email address.
  if( $_POST['email'] && !preg_match( "/.+@.+\..+/i", $_POST['email']) ) {
    $headers = "From: $_POST[email]";
  } else {
    $headers = "From: $youremail";
  }
  // finally, send the message
  mail($youremail, 'Contact Form', $body, $headers );
}

?>

ajax请求不会在代码中发送任何表单数据。您应该像这样添加表单数据:

var onSubmit = function(token) {
  var formData = $("#yourFormId").serialize();

  $.ajax({
     type: 'POST',
     data: formData,
     url: 'assets/php/contact.php',
     success: function(){
       alert('Thank you for your request ' + document.getElementById('name').value);
     }
   });
};

你说表格没有处理是什么意思?在您的示例中,您没有向php脚本发送任何数据。表单数据由contact.php处理。我想让contact.php做这个处理。我试过使用POST和GET,但都不起作用。如果我根本不使用Google reCaptcha,那么表单“action=”“”调用contact.php,并且工作正常。在ajax调用中,您没有向post添加任何表单字段值。您能给我们看一下您的表单和contact.php文件的代码吗。@Selimhamud-编辑问题以显示代码。OK-消息似乎正在处理中。:)那很好。
<script type="text/javascript">

      var onSubmit = function(token) {
        var formData = $("#form").serialize();
        $.ajax({
          type: 'POST',
          data: formData,
          url: 'assets/php/contact.php',
          success: function(){
            alert('Thank you for your request ' + document.getElementById('name').value);
          }
        });
      };

      var onloadCallback = function() {
        grecaptcha.render('mc-embedded-contact', {
          'sitekey' : '6Ldbfg8UAAAAAAaWVBiyo4uGfDqtfcnu33SpOj6P',
          'callback' : onSubmit
        });
      };
    </script>
var onSubmit = function(token) {
  var formData = $("#yourFormId").serialize();

  $.ajax({
     type: 'POST',
     data: formData,
     url: 'assets/php/contact.php',
     success: function(){
       alert('Thank you for your request ' + document.getElementById('name').value);
     }
   });
};