Ajax从其他php文件联系表单发送数据

时间:2016-08-27 12:22:35

标签: javascript php jquery ajax

我是AJAX的新手,我来自互联网上关于将Ajax发送到php文件的代码。问题是我不知道数据是否已提交或发生了什么。警报框如果被提交则不会弹出

success: function () {
              alert('form was submitted');
            }

以下是html的完整表格:

    <form class="form-horizontal">
    <div class="form-group">
    <label for="contact-name" class="col-sm-2 control-label">Name</label>
     <div class="col-sm-3">
    <input type="text" class="form-control" id="name" name="name" placeholder="FULL Name" required>
     </div>
     </div>
     <div class="form-group">
  <label for="inputEmail3" class="col-sm-2 control-label">Email</label>
   <div class="col-sm-4">
  <input type="email" class="form-control" id="email" name="email" placeholder="Email" required>
  </div>
  </div>
  <div class="form-group">
  <label for="contact-msg" class="col-sm-2 control-label">Message</label>
  <div class="col-sm-4">
  <textarea name="message" class="form-control" required></textarea>
    </div>
   </div>
   <div class="form-group">
   <div class="col-sm-offset-2 col-sm-10">
  <button class="btn btn-primary" type="submit" value="submit" >Send Message</button>
   </div>
   </div>
  </form>

这是脚本:

<script>
      $(function () {

        $('form').on('submit', function (e) {

          e.preventDefault();

          $.ajax({
            type: 'post',
            url: 'mail.php',
            data: $('form').serialize(),
            success: function () {
              alert('form was submitted');
            }
          });

        });

      });
</script>

因为我不知道问题出在哪里,请检查我的PHP代码。谢谢stackoverflow,优秀的程序员。 php代码:

<?php

    $name = $_POST['name']; 
    $email_from = $_POST['email']; 
    $comments = $_POST['message']; 

    $email_to = "jcjohn@gmail.com";
    $email_subject = "Message from John Web";
    $email_message = "Form details below.\n\n";

        function clean_string($string) {
          $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
        }

        $email_message .= "Name: ".clean_string($name)."\n";
        $email_message .= "Email: ".clean_string($email_from)."\n";
        $email_message .= "Comments: ".clean_string($comments)."\n";

        // create email headers
      $headers = 'From: '.$email_from."\r\n".
      'Reply-To: '.$email_from."\r\n" .
      'X-Mailer: PHP/' . phpversion();
       mail($email_to, $email_subject, $email_message, $headers);


        echo "<script type='text/javascript'>alert('$message');</script>";        
?>
我正在做对吗?或者我正在做一些无所事事的事情。

1 个答案:

答案 0 :(得分:0)

请使用ajaxForm插件并包含jquery库。我认为这对你来说很好。

<html> 
    <head> 
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
        <script src="http://malsup.github.com/jquery.form.js"></script> 

        <script> 
            // wait for the DOM to be loaded 
            $(document).ready(function() { 
                // bind 'myForm' and provide a simple callback function 
                $('#myForm').ajaxForm(function(data) { 
                    alert(data); 
                }); 
            }); 
        </script> 
    </head>
    <body>
    <form id="myForm" action="comment.php" method="post"> 
        <div class="form-group">
    <label for="contact-name" class="col-sm-2 control-label">Name</label>
     <div class="col-sm-3">
    <input type="text" class="form-control" id="name" name="name" placeholder="FULL Name" required>
     </div>
     </div>
     <div class="form-group">
  <label for="inputEmail3" class="col-sm-2 control-label">Email</label>
   <div class="col-sm-4">
  <input type="email" class="form-control" id="email" name="email" placeholder="Email" required>
  </div>
  </div>
  <div class="form-group">
  <label for="contact-msg" class="col-sm-2 control-label">Message</label>
  <div class="col-sm-4">
  <textarea name="message" class="form-control" required></textarea>
    </div>
   </div>
   <div class="form-group">
   <div class="col-sm-offset-2 col-sm-10">
  <button class="btn btn-primary" type="submit" value="submit" >Send Message</button>
   </div>
   </div>
    </form>
    </body>
    </html> 

并且您的PHP文件代码必须像这样

<?php

    $name = $_POST['name']; 
    $email_from = $_POST['email']; 
    $comments = $_POST['message']; 

    $email_to = "jcjohn@gmail.com";
    $email_subject = "Message from John Web";
    $email_message = "Form details below.\n\n";

        function clean_string($string) {
          $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
        }

        $email_message .= "Name: ".clean_string($name)."\n";
        $email_message .= "Email: ".clean_string($email_from)."\n";
        $email_message .= "Comments: ".clean_string($comments)."\n";

        // create email headers
      $headers = 'From: '.$email_from."\r\n".
      'Reply-To: '.$email_from."\r\n" .
      'X-Mailer: PHP/' . phpversion();
       mail($email_to, $email_subject, $email_message, $headers);


        echo "Form Submitted";        
?>