29 September 2013

主要使用了jQuery ajax - serialize() 方法.

The .serialize() method creates a text string in standard URL-encoded notation.

It can act on a jQuery object that has selected individual form controls, such as <input>, <textarea>, and <select>: $( "input, textarea, select" ).serialize();

html代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <script>
            $(function(){
                $('#myForm').on('submit',function(e){
                    $.ajax({
                        url:'./ajaxSubmit.php',
                        data:$(this).serialize(),
                        type:'POST',
                        success:function(result){
                            $('#var_dump_POST').append(result);
                        }
                    });
                return false;
            });
            });
        </script>
    </head>
    <body>
        <form action="" method="post" id="myForm">
            <label for="username">username:</label>
            <input type="text" name="username">
            <input type="submit">
        </form>
        <div id="var_dump_POST"></div>
    </body>
</html>

php代码:

1
2
3
<?php
    var_dump($_POST);
?>