0

我正在尝试使用 Jquery 和 AJAX 在不刷新页面的情况下提交一个小型联系表单。我从另一个 Stackoverflow 线程获得了代码,但是当我尝试提交表单并单击提交按钮时没有任何反应。我什至在控制台上也没有收到任何错误消息。所以任何人都可以告诉我我在这里做错了什么。这是表格

 <form id="contactform" name="contactForm">
       <input type="text" name="name"/><br/>
       <input type="text" name="email"/><br/>
      <textarea name="comment">

      </textarea>
      <p style='text-align:right;'><input type="submit"/></p>
       </form>

这是JS:

<script>
// variable to hold request
var request;
// bind to the submit event of our form
$("#contactform").submit(function(event){
    // abort any pending request
    if (request) {
        request.abort();
    }
    // setup some local variables
    var $form = $(this);
    // let's select and cache all the fields
    var $inputs = $form.find("input, select, button, textarea");
    // serialize the data in the form
    var serializedData = $form.serialize();

    // let's disable the inputs for the duration of the ajax request
    $inputs.prop("disabled", true);

    // fire off the request to /form.php
    request = $.ajax({
        url: "form.php",
        type: "post",
        data: serializedData
    });

    // callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        // log a message to the console
        console.log("Hooray, it worked!");
    });

    // callback handler that will be called on failure
    request.fail(function (jqXHR, textStatus, errorThrown){
        // log the error to the console
        console.error(
            "The following error occured: "+
            textStatus, errorThrown
        );
    });

    // callback handler that will be called regardless
    // if the request failed or succeeded
    request.always(function () {
        // reenable the inputs
        $inputs.prop("disabled", false);
    });

    // prevent default posting of form
    event.preventDefault();
});

</script>

最后是 post.php 文件。

<?php
echo $_POST['fullname']."<br/>";
echo $_POST['email']."<br/>";
echo $_POST['comment']."<br/>";

?>

这是具有以下形式的页面的网址:http: //contestlancer.com/davidicus/

如果您单击标题徽标中的小消息图标,您将看到联系表。

问候艾哈迈尔

4

1 回答 1

5

您正在发送请求,form.php 并且您说您的文件名为post.php. 更改此部分:

request = $.ajax({
        url: "form.php",
        type: "post",
        data: serializedData
    });

至:

request = $.ajax({
        url: "post.php",
        type: "post",
        data: serializedData
    });
于 2013-08-31T19:29:00.020 回答