Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP read Javascript array

Tags:

javascript

php

I am passing an array from Javascript to PHP page, as below.

var arrF1 = [{"Item":"item1no",
      "Desc":"item1desc",
      "Remarks":"item1note"},
             {"Item":"item2no",
      "Desc":"item2desc",
      "Remarks":"item2note"}
    ];
$.ajax({        
   type: "POST",
   url: "http://www.mydomain.com/pdfs/flist.php",
   data: { favArray : arrF1 },
   success: function() {
        alert('ok, sent');
   }
}); 

In my PHP page, I read the array as below.

$fArray = json_decode($_POST['favArray'])

And I tried to access the arrays value like this.

$fArrav[0]->{'Item'}
$fArrav[0]->{'Desc'}
$fArrav[1]->{'Item'}

Is this correct? I am generating a PDF on the server using FPDF. But with the above, its not reading the array.

I must not be doing this right. Please help.

Thank you.

like image 481
user1509446 Avatar asked Jul 11 '12 20:07

user1509446


1 Answers

With this PHP:

function handlePost() {
    $a = $_POST['favArray'];
    var_dump($a);
}

..and this Javascript:

function post() {
    var a1 = [ 
        {"Item":"109249383",
         "Desc":"item1desc",
         "Remarks":"item1note"},

        {"Item":"298298210",
         "Desc":"item2desc",
         "Remarks":"item2note"}
    ];

    $.ajax({
        type: "POST",
        url: "readArray.php",
        data: { favArray : a1 },
        success: function() {
            alert1('ok, sent');
        }
    });
}

...I get this output:

HTTP/1.1 200 OK
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-Powered-By: PHP/5.3.10
Date: Wed, 11 Jul 2012 21:04:16 GMT
Content-Length: 315

array(2) {
  [0]=>
  array(3) {
    ["Item"]=>
    string(9) "109249383"
    ["Desc"]=>
    string(9) "item1desc"
    ["Remarks"]=>
    string(9) "item1note"
  }
  [1]=>
  array(3) {
    ["Item"]=>
    string(9) "298298210"
    ["Desc"]=>
    string(9) "item2desc"
    ["Remarks"]=>
    string(9) "item2note"
  }
}

In this case, the encoding of the data on the wire is not JSON. It is 'application/x-www-form-urlencoded' as per the default used by jQuery's ajax function. Looks like this:

favArray=%5B%7B%22Item%22%3A%22109249383%22%2C%22Desc%22%3A%22item1desc%22%2C
    %22Remarks%22%3A%22item1note%22%7D%2C%7B%22Item%22%3A%22298298210%22%2C%22
    Desc%22%3A%22item2desc%22%2C%22Remarks%22%3A%22item2note%22%7D%5D

(all on one line)
Therefore it does not make sense to call json_decode within PHP - there was never any JSON involved. PHP decodes the url-encoded post body automatically.


If you want to encode with JSON, then you can use JSON.stringify() directly. This may require json2.js on the browser side. (see this answer)

To use JSON, then you would need something like this in the browser:

function post_json_encoded() {
    $.ajax({
        type: "POST",
        url: "postArray.php",
        contentType: 'application/json', // outbound header
        dataType: 'text',                // expected response
        data: JSON.stringify(a1),        // explicitly encode
        success: function() {
            alert1('ok, json sent');
        }
    });
}

...and then something like this on the php side:

function handlePost() {
    header('Content-Type: text/plain; charset="UTF-8"');
    $post_body = file_get_contents('php://input');
    $a = json_decode($post_body);
    var_dump($a);
}

In this case the on-the-wire representation looks like this:

[{"Item":"109249383","Desc":"item1desc","Remarks":"item1note"},
 {"Item":"298298210","Desc":"item2desc","Remarks":"item2note"}]

...and the php var_dump output is this:

array(2) {
  [0]=>
  object(stdClass)#1 (3) {
    ["Item"]=>
    string(9) "109249383"
    ["Desc"]=>
    string(9) "item1desc"
    ["Remarks"]=>
    string(9) "item1note"
  }
  [1]=>
  object(stdClass)#2 (3) {
    ["Item"]=>
    string(9) "298298210"
    ["Desc"]=>
    string(9) "item2desc"
    ["Remarks"]=>
    string(9) "item2note"
  }
}
like image 52
Cheeso Avatar answered Oct 04 '22 21:10

Cheeso