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);
 ?>

22 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
  7. I appreciate you sharing articles, thank you for your efforts in this article

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

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

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

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

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

    ReplyDelete
  13. Great! Thanks for sharing the information. That is very helpful for increasing my knowledge in this field.
    pacman |happy wheels |my little pony games | unblocked games
    friv4 kizi 4| unblockedgames

    ReplyDelete
  14. When you see the stress in work, life, friv could well be a good site to try. thanks you for sharing
    Friv Games, Friv Online, Friv.com, Friv

    ReplyDelete
  15. Thanks for sharing the information. It is very useful for my future. keep sharing
    Friv 1
    Juegos Friv 3

    ReplyDelete
  16. Play free Friv games flash online games flash.
    Thanks you for sharing
    Friv Games | Friv Online

    ReplyDelete
  17. You need to kill time, you need entertainment. Refer to our website. hope you get the most comfort.
    Thanks you for sharing!
    Friv
    Friv Games
    Yepi 4
    Kizi 4

    ReplyDelete
  18. Nice read. Very good post for fun and some time pass. Thanks for sharing.
    Kizi 2
    Friv 2

    ReplyDelete
  19. Appreciate your sharing. I understand what you bring it very meaningful and useful, thanks.
    Signature:
    i like play happy wheels demo online and play happy wheels games full and friv4 , girls go games , games 2 girls

    ReplyDelete
  20. you'd have time to look these kids active. Please visit our website and let us play the game interesting.
    Thanks for sharing !
    Friv 10
    Kizi 10
    Yepi 2

    ReplyDelete
  21. after hours of work tired, you need entertainment. Refer to our website. hope you get the most comfortable
    Kizi 1000

    ReplyDelete
  22. You need to kill time, you'd have time to look these kids active. Refer to our website. Hope you get the most comfort.
    Thanks for sharing !
    Friv 5
    Kizi 1
    Yepi 3

    ReplyDelete