0

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...

1
  • try print_r($_GET['mapIDs']); Commented Apr 17, 2012 at 20:57

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Didn't work – PHP is still not returning anything (PHP error log says "undefined index" for mapIDs.
@jrbaldwinn: print_r($_GET) to see if anything is being sent at all.
1

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.

4 Comments

You can't have 2 elements in one object with the same name.
You're (mostly) right. I could set traditional: false, as is the default, and mapIDs[]: mapIDArray.
You shouldn't have the [] in the name with traditional: false.
You forgot the {} around the object passed to $.ajax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.