Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8217531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:29:37+00:00 2026-06-07T12:29:37+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:29:38+00:00Added an answer on June 7, 2026 at 12:29 pm

    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"
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble passing an array from controller to a javascript in a
I am passing a PHP array to a javascript function. The only way I
I have found Passing an array from Javascript to C++ solution, but I have
I'm passing an array from my Rails app to Javascript via a js.erb template.
Am trying to pass an array of values from PHP function to Javascript. Not
I need to pass an array from JavaScript to a page method in C#.
I am attempting to use JSONP to return an array from PHP to JavaScript.
Hi i'm trying to retrieve response after passing some variables from javascript to php
Possible Duplicate: Passing JavaScript Array To PHP Through JQuery $.ajax I'm trying to pass
I'm passing from the controller an array generated by the next code: public ActionResult

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.