尚未通过AJAX设置$ _POST?

时间:2018-08-01 10:18:33

标签: php

因此,我试图通过javascript将联系表中的一些信息发送到php函数。下面的代码段:

$(function() {
    // Get the form.
    var form = $('#contact_form');

    // Get the messages div.
    var formMessages = $('#form-messages');

    // Set up an event listener for the contact form.
    $(form).submit(function(event) {

      // Stop the browser from submitting the form.
      event.preventDefault();
      
      // Serialize the form data.
      var formData = $(form).serialize();
      alert($(form).attr('action'));
      // 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.
            $('#name').val('');
            $('#email').val('');
            $('#message').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.');
            }
        });
        
    });	
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="contact_form" method="post" class="form" role="form" action="/mailer.php">
    <div class="row">
        <div class="col-md-6 form-group">
            <label for="name"> Your name:</label>
            <input class="form-control" id="name" name="name" placeholder="Name" type="text" required maxlength="50" />
        </div>
        <div class="col-md-6 form-group">
            <label for="email"> Your email address:</label>
            <input class="form-control" id="email" name="email" placeholder="Email" type="email" required maxlength="50" />
        </div>
    </div>
    <div class="form-group">
      <label for="message"> Your message:</label>
      <textarea class="form-control" id="message" name="message" placeholder="Message" maxlength="6000" rows="7" required></textarea>
    </div>
    <br />
    <div class="row">
        <div class="col-md-12">
            <!-- <input type="submit" name="submit" value="Submit">-->
            <button class="btn btn-lg btn-success pull-right" name="submit" type="submit">Submit</button>
        </div>
    </div>
</form>
<div id="form-messages"></div>

<?php

//only process POST 
if(isset($_POST['submit'])){

    // This is in the PHP file and sends a Javascript alert to the client
    $message = "it is a post";
    echo "<script type='text/javascript'>alert('$message');</script>";

    //do something

} 
else {

    // Not a POST request, set a 403 (forbidden) response code.
    http_response_code(403);
    echo "There was a problem with your submission, please try again.";

    // This is in the PHP file and sends a Javascript alert to the client
    $message = "not POST";
    echo "<script type='text/javascript'>alert('$message');</script>";

    if(isset($_GET['submit'])) {
    echo "is GET";}
}?>

当我提交表单时,我知道js正在工作,因为我在$ .ajax({行之前)弹出警报。

但是,然后php函数回显“您的提交存在问题,请重试。”暗示sumbit方法不是POST吗?但后来我再也看不到“是GET”回声...

我已经检查了chrome开发人员模式->网络,并且表单提交确实确实是POST。所以我对可能出什么问题迷失了。

有人可以帮忙吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

  

是否暗示sumbit方法不是POST?

…因为不是。 serialize方法不包括提交按钮。 (提交按钮仅在用于提交表单时才是成功的控件,并且不提交表单,而是从中读取JavaScript的提交数据。)

更改您的PHP以使用其他测试来查看是否正在发布表单数据。