I have an AJAX call in the following format:
$.get('/controller/method/',
{
parameter1: value1,
parameter2: value2
},
function(data) {
if (data) {
}
else {
}
});
Is it possible to pass an array as a parameter?
parameter3: new Array(1, 2, 3)
parameter4: new Array('one' => 1, 'two' => 2, 'three' => 3)
You will probably need to name your variable as "parameter3[]" for PHP's sake:
$.get('/controller/method/',
{
"parameter1": "value1",
"parameter2": "value2",
"parameter3[]": ["a","b","c"]
},
function(data) {
if (data) {
}
else {
}
});
$_GET["parameter3"] will appear in PHP as
Array
(
[0] => "a"
[1] => "b"
[2] => "c"
)
I've been down this road before. Join your array with a comma (or whatever character will work best for your scenario) and send it as a single parameter...
var arr = [5, "x", 25];
var parms = {
parameter1: "value1",
parameter2: arr.join(",");
}
And on the server-side, your "parameter2" post variable will look like 5,x,25
This is an easy solution for both sides of the wire.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With