Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Passing array to PHP

    Hi, I want to use ajax to pass an array from the javascript in my web page to PHP so that the php can then use it, I want to do this without reloading the page and the array could be quite large so I think i need to use POST. I have been doing one searches and have only seen GET used. Can someone give me a very simple example of how you would pass an array using ajax-POST to PHP? Thanks

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    Code:
    <!doctype html>
    <head>
    </head>
    <body>
     
    <script> 
    myArray=[];
    myArray[0]="zero";
    myArray[1]="one";
    req=new XMLHttpRequest;
    req.open("post","arrayto.php",true)
    req.onreadystatechange=function(){
      if (req.readyState==4){
        alert(req.responseText)
        }
      }
    params='myArray='+myArray;
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    req.setRequestHeader("Content-length", params.length);
    req.setRequestHeader("Connection", "close");
    req.send(params);
     
    </script>
    </body>
    </html>
    on the server ...
    $myArray=explode(",",$_POST["myArray"]);
    echo "Got it";
    Last edited by DaveyErwin; 06-13-2012 at 12:26 AM.

  • Users who have thanked DaveyErwin for this post:

    bigpondtinyfish (06-13-2012)

  • #3
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks, that was perfect.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •