Jq post() 傳遞數組給php

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="./src/jquery-1.8.3.min.js"></script>

</head>
<body>

<form action="">
    <div>
        <input type="text" name="name1" value="zs">
        <input type="text" name="age1" value="22">
        <input type="text" name="desc1" value="zs is sb">
    </div>

    <div>
        <input type="text" name="name2" value="ls">
        <input type="text" name="age2" value="19">
        <input type="text" name="desc2" value="ls is bc">
    </div>

    <div>
        <input type="text" name="name3" value="zw">
        <input type="text" name="age3" value="24">
        <input type="text" name="desc3" value="zw is rz">
    </div>

    <input id="Submit" type="button" value="提交">
</form>


<script>
    $(function(){

        $('#Submit').click(function(){
            var arrList = [];
            var arr1 = [
                $('input[name="name1"]').val(),
                $('input[name="age1"]').val(),
                $('input[name="desc1"]').val()
            ];
            var arr2 = [
                $('input[name="name2"]').val(),
                $('input[name="age2"]').val(),
                $('input[name="desc2"]').val()
            ];
            var arr3 = [
                $('input[name="name3"]').val(),
                $('input[name="age3"]').val(),
                $('input[name="desc3"]').val()
            ];

            arrList.push(arr1,arr2,arr3);

            var dataJson = {data : JSON.stringify(arrList)}
            $.post("index.php",dataJson,function(rs){
            });
        });
    })  
</script>
    
</body>
</html>

php

<?php

$data = $_POST['data'];
$data = json_decode($data,true);

echo '<pre>';
var_dump($data);

//var_dump結果:php

/home/vagrant/code/test/index.php:7:
array (size=3)
0 =>
array (size=3)
0 => string 'zs' (length=2)
1 => string '22' (length=2)
2 => string 'zs is sb' (length=8)
1 =>
array (size=3)
0 => string 'ls' (length=2)
1 => string '19' (length=2)
2 => string 'ls is bc' (length=8)
2 =>
array (size=3)
0 => string 'zw' (length=2)
1 => string '24' (length=2)
2 => string 'zw is rz' (length=8)html

相關文章
相關標籤/搜索