1

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"
  }
}
7
  • Try var_dump($_POST['ajaxArray']) and var_dump( json_decode($_POST['ajaxArray']) ) in order to see what happens. Commented Apr 26, 2014 at 16:42
  • how can I do that? Consider that in this form when I click the submit button, the array is submitted and wait for the response (without refresh/change page)...and I'm able to see only the console.log(data) Commented Apr 26, 2014 at 16:56
  • Just put the var_dump(..), and a return afterwards. Then use Firebug (firefox extension), or the normal console, to see the response of your Ajax requests. Commented Apr 26, 2014 at 17:06
  • Post edited. It seems that everything is all right, but ther's no difference between the two var_dump Commented Apr 26, 2014 at 17:34
  • My bad...re-edited post with the two var_dump Commented Apr 26, 2014 at 17:40

1 Answer 1

1

in the first example you're using associative arrays, however the return from the client is json_decoded and the associative arrays are turned into Objects. I suspect that you did not account for this in your loop.

$returnAjax = array();
foreach ($ajaxArray as $value){
    // not this - $value['from'];
    // this
    $value->from;   
}
Sign up to request clarification or add additional context in comments.

2 Comments

You rock, i love you :)...headacke for 2 weeks!!
no problem. sometimes it just needs a fresh set of eyes. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.