i'm tryng to create an Ajax/Php form. The server-side works perfect infact the print_r print exactly what i expect. In this "version" of the .php file I directly create the ajaxArray.
$ajaxArray = array(0 => array(from => "01/01/2010",
to => "01/01/2011",
amount => 1000.00),
1 => array(from => "01/01/2011",
to => "01/01/2012",
amount => 100.00),
2 => array(from => "01/01/2012",
to => "01/01/2013",
amount => 100.00)
);
$returnAjax = array();
foreach ($ajaxArray as $value){
//... Here we do some operations and push result into $returnAjax ...
}
print "<pre>";
print_r($returnAjax);
print "</pre>";
My issue is when i try to pass the ajaxArray through Ajax.
Client-side:
var ajaxArray = //Some function to create the array
ajaxData = {ajaxArray: JSON.stringify(ajaxArray)};
console.log(ajaxData) //OK, the ajaxArray have the right values
$.ajax({
type: 'POST',
url: '..url-to-php-file',
data: ajaxData,
dataType: 'json',
success: function (data) {
console.log(data);
}
});
This is the console.log(ajaxData)
:
ajaxArray: "[{"from":"01/01/2010","to":"01/01/2011","amount":"1000.00"},{"from":"01/01/2011","to":"01/01/2012","amount":"100.00"},{"from":"01/01/2012","to":"01/11/2013","amount":"100.00"}]"
Server-side. This is the changed version of the same .php
file. As you can see the $ajaxArray take value from $_POST
, but the $returnAjax
return an empty response
$ajaxArray = json_decode($_POST['ajaxArray']);
$returnAjax = array();
foreach ($ajaxArray as $value){
/*... Here we do the same operations as
before and push result into $returnAjax ...*/
}
return json_encode($returnAjax);
It seems that the foreach loop doesn't work when values are passed through Ajax, or something else goes wrong.
Thanks in advance
EDIT: In italian Da=from, A=to, Importo=amount
That's the console log of var_dump($_POST['ajaxArray']);
string(218) "[{"Da":"01/01/2010","A":"01/01/2011","Movimento":"A","Importo":"1000.00"},{"Da":"01/01/2011","A":"01/01/2012","Movimento":"D","Importo":"100.00"},{"Da":"01/01/2012","A":"26/04/2014","Movimento":"D","Importo":"100.00"}]"
That's the console log of var_dump( json_decode($_POST['ajaxArray']) )
string(218) "[{"Da":"01/01/2010","A":"01/01/2011","Movimento":"A","Importo":"1000.00"},{"Da":"01/01/2011","A":"01/01/2012","Movimento":"D","Importo":"100.00"},{"Da":"01/01/2012","A":"26/04/2014","Movimento":"D","Importo":"100.00"}]"
array(3) {
[0]=>
object(stdClass)#5 (4) {
["Da"]=>
string(10) "01/01/2010"
["A"]=>
string(10) "01/01/2011"
["Movimento"]=>
string(1) "A"
["Importo"]=>
string(7) "1000.00"
}
[1]=>
object(stdClass)#6 (4) {
["Da"]=>
string(10) "01/01/2011"
["A"]=>
string(10) "01/01/2012"
["Movimento"]=>
string(1) "D"
["Importo"]=>
string(6) "100.00"
}
[2]=>
object(stdClass)#7 (4) {
["Da"]=>
string(10) "01/01/2012"
["A"]=>
string(10) "26/04/2014"
["Movimento"]=>
string(1) "D"
["Importo"]=>
string(6) "100.00"
}
}
var_dump($_POST['ajaxArray'])
andvar_dump( json_decode($_POST['ajaxArray']) )
in order to see what happens. – Linblow Apr 26 at 16:42var_dump(..)
, and areturn
afterwards. Then use Firebug (firefox extension), or the normal console, to see the response of your Ajax requests. – Linblow Apr 26 at 17:06