douzheng5717 2013-08-31 19:22
浏览 40
已采纳

通过AJAX提交表单无法正常工作

I am trying to use Jquery and AJAX to submit a small contact form without page refresh. I got the code from another Stackoverflow thread but when I try to submit a form and click submit button nothing happens. I don't even get any error messages on console also . So can any one tell me what I am doing wrong here. Here is the form

 <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>

Here is 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>

and finally here is post.php file.

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

?>

Here is the url of the page which has the form: http://contestlancer.com/davidicus/

If you click on the small message icon in the header logo you will see contact form.

Regards Ahmar

  • 写回答

1条回答 默认 最新

  • douba8819 2013-08-31 19:29
    关注

    You're sending request to form.php and you said your file name is post.php. Change this part:

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

    To:

    request = $.ajax({
            url: "post.php",
            type: "post",
            data: serializedData
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来