Friday, 4 April 2014

How to pass a Javascript Array to PHP file using AJAX and JSON?

How to pass a Javascript Array to PHP file using AJAX and JSON?

I have an array in javascript and I need to pass this array to a PHP by using AJAX call to that PHP file. I will get this array in PHP file and assign this javascript array to PHP array. Then I will find out the count of that PHP array elements and return it back to the javascript. I will convert JS array in JSON format by JSON.stringify. This is a very simple example on how to pass javascript array to PHP asynchronously. You can perform a lot of operations on this array which you have passed to PHP file but for simplicity I am just returning its count. Let's have a look at the following code snippet.

Javascript Array

var myJSArray = new Array("Saab","Volvo","BMW");

ProcessAJAXRequest() function wil pass JS array to PHP file using AJAX request and will show count of array elements returned by PHP file.

function ProcessAJAXRequest()
        type: "POST",
        url: "myphpfile.php",
        data: {"myJSArray" : JSON.stringify(myJSArray)},
        success: function (data) 
            alert(data); //count of array elements


    $myPHPArray = json_decode($_POST["myJSArray"]);
    echo count($myPHPArray);


  1. I am a beginner in php, your article helped me a lot, I need it. Thanks for sharing

  2. thank you have a good article, it's nice I've read it, and that I need it, thank you

  3. can book the perfect place, it helps a lot in everyday life, I want to learn in this, thank you

  4. +Handling PhP:
    1.tao 1 baseURL variable to get the current url of the file is run
    2.Using $ _POST request from the client to check (using ajax)
    3.sleep (1) / / temporary for 1 second before running to stop => this so you can see clearly the process
    4.Create 1 array member contains the information of members
    5.Using function json_encode (), the goal is to move the newly created array into json string returned to the Client
    6.sau it die: the end is not running anymore (stop after every process returns data)
    +Handling Ajax:
    1. The url: php file path will process your request
    2. Type: The type of data you send to the server (POST, GET)
    3. Data: data is what you post? (GetMember = view)
    4. Success: when the server successfully processed will return here, where you will handle JSON string parsing, and enter data into the textbox.
    => $ User function. ParseJSON (string) to parse JSON string, that string is in the block of data returned from the server.

  5. Burberry Outlet Air Max Shoes Coach Black Friday Coach Black Friday Coach Cyber Monday Coach Factory Coach Outlet Factory Coach Outlet USA Coach Purses Factory Coach Purses USA Coach Store Outlet Coach Purses On Sale Monster Beats Outlet Louis Vuitton Outlet Louis Vuitton Factory Marc Jacobs On Sale MCM Outlet MCM 店铺 cheap oakley sunglasses Michael Kors Outlet Michael Kors Outlet Michael Kors Factory Michael Kors Outlet Michael Kors Factory Online Coach Factory Outlet North Face Outlet Online Polo Outlet Store Ralph Lauren UK Sac Longchamp Pairs Coach Outlet Online Coach Factory Oultet Barbour Jackets Outlet Online Canada Goose Outlet Gucci Outlet Online Michael Kors Outlet Moncler Clearance Moncler Jackets Outlet Online North Clearace Outlet Polo Ralph Lauren Outlet Online Woolrich Clearance Michael Kors Factory Outlet Coach Outlet Online USA Beats by Dre Coach Purses Outlet Online Michael Kors USA

  6. Your article or, I like and need it. Thank you

  7. I appreciate you sharing articles, thank you for your efforts in this article

  8. the great thing is that I need it in their daily work, thank you for that

  9. What you share always gives me a lot of fun and good luck, thanks

  10. Thanks for the info! Is there a way I can tell my people about this post

  11. Make women rational creatures, and free citizens, and they will quickly become good wives; - that is, if men do not neglect the duties of husbands and fathers.

  12. Absences are a good influence in love and keep it bright and delicate.
    friv 2 | frip 2