Ajax POST无法使用Jquery和PHP

时间:2012-09-23 14:51:36

标签: php ajax jquery post

我是Jquery的新手。我试图使用Jquery Post将表单发布到php文件,我在php文件中做了一个var_dump($ _ POST)但我只能看到一个空数组(看到使用firebug net),但我回到成功Jquery的功能。可能是我在做一些愚蠢的事情。除了这个问题,所有Javascript行为都按预期工作。有人请帮忙

$(document).ready(function() {

    //if submit button is clicked
    $('#submit').click(function () {       

        //show the loading sign
        $('#example').modal('show') ; 


        //start the ajax
        $.ajax({
            //this is the php file that processes the data 
            url: "mvc/process_form.php",

            //GET method is used
            type: "POST",


            //Do not cache the page
            cache: false,

            //success
            success: function (html) {             
                //if process_form.php returned 1/true (process success)

                if (html==1) {                 
                    //hide the form
                    $('.form').fadeOut('slow');                

                    //show the success message
                    $('.done').fadeIn('slow');

                //if process.php returned 0/false (send mail failed)
                } else alert('Sorry, unexpected error. Please try again later.');              
            }      
        });

        //cancel the submit button default behaviours
        return false;
    });
}); 

3 个答案:

答案 0 :(得分:2)

你没有在你的ajax函数中传递任何$ _POST datahttp://api.jquery.com/jQuery.ajax/

答案 1 :(得分:1)

您需要将表单数据作为序列化传递。现在你没有将表单数据传递到你的ajax函数。

你走了:

$(document).ready(function() {

    //if submit button is clicked
    $('#submit').click(function () {       

        //show the loading sign
        $('#example').modal('show') ; 


        //start the ajax
        $.ajax({
            //this is the php file that processes the data 
            url: "mvc/process_form.php",

            //GET method is used
            type: "POST",
            data: $("#testform").serialize(),

            //Do not cache the page
            cache: false,

            //success
            success: function (html) {             
                //if process_form.php returned 1/true (process success)

                if (html==1) {                 
                    //hide the form
                    $('.form').fadeOut('slow');                

                    //show the success message
                    $('.done').fadeIn('slow');

                //if process.php returned 0/false (send mail failed)
                } else alert('Sorry, unexpected error. Please try again later.');              
            }      
        });

        //cancel the submit button default behaviours
        return false;
    });
}); 

答案 2 :(得分:0)

也可以尝试:

var url = "mvc/process_form.php", 
   data = {hello: hello}; 
$.post(url,data, 
      function (status)
      {
         alert(status);
      }
 );