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()
{
    $.ajax
    ({
        type: "POST",
        url: "myphpfile.php",
        data: {"myJSArray" : JSON.stringify(myJSArray)},
        success: function (data) 
        {
            alert(data); //count of array elements
        }
    });
}

myphpfile.php

<?php 
    $myPHPArray = json_decode($_POST["myJSArray"]);
    echo count($myPHPArray);
 ?>

6 comments:

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

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

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

    ReplyDelete
  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.

    ReplyDelete
  5. http://www.burbagssale2013.com/ Burberry Outlet
    http://www.airmaxshoesfactory.com/ Air Max Shoes
    http://www.coachblackfriday2014.com/ Coach Black Friday
    http://www.coach-storeoutletonline.com/ Coach Black Friday
    http://www.coachcoachoutlet.com/ Coach Cyber Monday
    http://www.coachxfactory.com/ Coach Factory
    http://www.coach-factoryoutletonline.net/ Coach Outlet Factory
    http://www.coach-outletonlineusa.com/ Coach Outlet USA
    http://www.coach-pursesfactory.com/ Coach Purses Factory
    http://www.coachpurseusa.com/ Coach Purses USA
    http://www.coach-storeoutlet.com/ Coach Store Outlet
    http://www.coach-pursesonline.com/ Coach Purses On Sale
    http://www.monsterbeatsbydres.com/ Monster Beats Outlet
    http://www.louis-vuittonblackfriday.com/ Louis Vuitton Outlet
    http://www.lv-guccishoesfactory.com/ Louis Vuitton Factory
    http://www.marcjacobsonsale.com/ Marc Jacobs On Sale
    http://www.mcmworldwides.com/ MCM Outlet
    http://www.mcmoutlet-jp.com/ MCM 店铺
    http://www.oakleysunglassesfactory.com/ cheap oakley sunglasses
    http://www.michaelkorsmas.com/ Michael Kors Outlet
    http://www.michaelkors.so/ Michael Kors Outlet
    http://www.michaelkorsfactory-store.com/ Michael Kors Factory
    http://www.michaelkorsoutletr.com/ Michael Kors Outlet
    http://www.michael-korsfactoryonline.com/ Michael Kors Factory Online
    http://www.newcoachfactoryoutlet.com/ Coach Factory Outlet
    http://www.north-faceoutletonlines.net/ North Face Outlet Online
    http://www.polo-outletstore.com/ Polo Outlet Store
    http://www.ralph-laurenhome.com/ Ralph Lauren UK
    http://www.saclongchamppairs.com/ Sac Longchamp Pairs
    http://www.tcoachoutletonline.com/ Coach Outlet Online
    http://www.the-coachfactoryoutlet.com/ Coach Factory Oultet
    http://www.barbour-jacketsoutlet.com/ Barbour Jackets Outlet Online
    http://www.canada-gooser.com/ Canada Goose Outlet
    http://www.guccishoesuk-2014.com/ Gucci Outlet Online
    http://www.michaelkorsstates.com/ Michael Kors Outlet
    http://www.moncler-clearance.com/ Moncler Clearance
    http://www.famousbagsmall.com/
    http://www.moncler-jacketsoutletonline.com/ Moncler Jackets Outlet Online
    http://www.northsclearance.com/ North Clearace Outlet
    http://www.polo-ralphlaurenonline.com/ Polo Ralph Lauren Outlet Online
    http://www.woolrich-clearance.com/ Woolrich Clearance
    http://www.cvshopfactory.com/ shop.coachfactory.com
    http://www.mksfactoryoutlet.com/ Michael Kors Factory Outlet
    http://www.zxcoachoutlet.com/ Coach Outlet Online USA
    http://www.thebeatsbydre.net/ Beats by Dre
    http://www.vipbagsmall.com/
    http://www.newoutletonlinemall.com/ Coach Purses Outlet Online
    http://www.clickmichaelkors.com/ Michael Kors USA

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

    ReplyDelete