简体   繁体   中英

jQuery $.getJSON : Passing arrays to PHP

I have javascript passing an array to PHP with:

         var mapIDArray = ["4f8d7684791635ec2e000000", "4f8cbc087916359181000000"];

         $.getJSON("rebound.php",
            { 
                'mapIDs[]' : mapIDArray
            },

            function(output){

                console.log(output);

            }
        );

In rebound.php, I try to read the passed array (var_dump, print_r, etc.), such as:

print_r($_GET['mapIDs[]']);

But no luck...

You don't need add the [] to the name.

var mapIDArray = ["4f8d7684791635ec2e000000", "4f8cbc087916359181000000"];
$.getJSON("rebound.php",
{ 
   mapIDs: mapIDArray
},
function(output){
    console.log(output);
});

Then in your PHP: $_GET['mapIDs'] will be an array.

It'd be print_r($_GET['mapIDs']); on the server-side, and

var mapIDArray = ["4f8d7684791635ec2e000000", "4f8cbc087916359181000000"];
$.ajax("rebound.php",
            dataType: 'json',
            data:
            { 
                'mapIDs[]' : mapIDArray[0],
                'mapIds[]' : mapIdArray[1],
            },
            traditional: true,
            success: function(output){

                console.log(output);

            }
        );

on the client side. The key issues being that first, PHP sees it an an array with name 'mapIDs', even though it was mapIDs[] as a GET parameter, and second, multiple fields need multiple entries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM