0

我是 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;
    });
}); 
4

3 回答 3

2

您没有data在 ajax 函数中传递任何 $_POST 。http://api.jquery.com/jQuery.ajax/

于 2012-09-23T14:55:14.197 回答
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;
    });
}); 
于 2012-09-23T14:56:21.997 回答
0

也试试这个:

var url = "mvc/process_form.php", 
   data = {hello: hello}; 
$.post(url,data, 
      function (status)
      {
         alert(status);
      }
 );
于 2012-09-23T14:56:43.890 回答