I have this jquery code:
var myarr = ["aa","ss","dd"];
$.ajax({
url: "proces.php",
data: "arr="+myarr,
type: "POST",
success: function () {
alert("data is send");
}
});
I see message data is send but in proces.php file I have this code
$str = '';
foreach ($_POST['arr'] as $k=>$v) {
$str = $str.$v;
}
$hand = fopen("t.txt","w+");
fwrite($hand,$str);
and in file t.txt nothing is written, please tell where I wrong ?
If you stringify an array and append it to
arr=then you get:but the standard way of sending an array of data over HTTP is:
PHP has an extra requirement. It expects the name to end in
[]Don’t try to build the form encoded data yourself. Let jQuery do it. It defaults to generating PHP style key names for arrays and will take care of any escaping needed due to characters which have special meaning in URIs (e.g. if your data included a
&).Change:
to
Then in PHP
$_POST['arr']will be an array.