Pass javascript array to php by using curly braces key name
Posted
by
user7031
on Stack Overflow
See other posts from Stack Overflow
or by user7031
Published on 2013-06-30T04:16:16Z
Indexed on
2013/06/30
4:21 UTC
Read the original article
Hit count: 210
My js code:
$(function(){
var arr = new Array('jj', 'kk', 'oo');
$.post('test12.php', {'arr[]': arr}, function(data){
alert(data);
});
});
PHP code:
<?php
echo print_r($_POST['arr']);
The thing is,$.post receive a key named 'arr[]',it should be used in PHP as 'arr[]' instead of 'arr',but '$_POST['arr[]']' doesn't work,'arr' works.Which seems that Jquery might do something with curly braces '[]' before sending something to PHP.
Secondly,when I remove the single quotas around 'arr[]',PHP can not receive anything by using $_POST['arr'];,I don't know why?
Doing this task in a traditional way with no curly braces:
$.post('test12.php', {arr: arr}, function(data){
alert(data);
});
It works fine.
So when sending javascript array to PHP,why bothering using single quote and curly braces like
'arr[]'
instead of using a concise way like arr:arr
My return result is
Array(
[0]=>jj
[1]=>kk
[2]=>oo
)
1
Notice there is a 1 under the array,why?
© Stack Overflow or respective owner