Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-12-2012, 06:30 PM   PM User | #1
bigpondtinyfish
New to the CF scene

 
Join Date: Jun 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
bigpondtinyfish is an unknown quantity at this point
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
bigpondtinyfish is offline   Reply With Quote
Old 06-12-2012, 11:04 PM   PM User | #2
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
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..
DaveyErwin is offline   Reply With Quote
Users who have thanked DaveyErwin for this post:
bigpondtinyfish (06-13-2012)
Old 06-13-2012, 03:12 AM   PM User | #3
bigpondtinyfish
New to the CF scene

 
Join Date: Jun 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
bigpondtinyfish is an unknown quantity at this point
Thanks, that was perfect.
bigpondtinyfish is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:39 PM.


Advertisement
Log in to turn off these ads.