2 Replies - 40503 Views - Last Post: 08 October 2008 - 05:59 AM

#1 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 245
  • Joined: 05-August 08

passing javascript array to ajax

Post icon  Posted 07 October 2008 - 10:49 PM

can we pass a java script array as an argument to php using ajax request.

i am trying the following code but it's not working.

can any one help me pls..

java script
<script type="text/javascript">

function showCalendar()
	var arr=new Array(1,2,3,4,5);

	if (xmlHttp==null){
		alert ("Browser does not support AJAX - Please update");
	var url="getArray.php";


function stateChanged() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {

function GetXmlHttpObject(){
	var xmlHttp=null;
	// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	catch (e){
	//Internet Explorer
		try	{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		catch (e){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	return xmlHttp;


<body onload="showCalendar()">
<div id="show">

php code
print count($arr)."<br>";

i want to catch the array from java script. Then i want to do some stuff with array and again i want to return back that array to java script.

is it possible.

thanks for any help

Is This A Good Question/Topic? 0
  • +

Replies To: passing javascript array to ajax

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1509
  • View blog
  • Posts: 8,270
  • Joined: 07-September 06

Re: passing javascript array to ajax

Posted 08 October 2008 - 05:45 AM

I believe the easiest way to do this is to concatinate the array into a string on the JS side and then send it and on the PHP side split it apart. Though, this would assume that things are number and not simply strings of characters. If you are passing string it could get a bit more complex.

So, here is what I am talking about:
var array = new Array(0, 1,2, 3, 4, 5, 6);
function concat(array){
  var str = "";
  for(i=0; i<array.length; i++){
	  str += ""+array[i];
	  str += ","+array[i];
  return str;

var string = concat(array); // should return "0,1,2,3,4,5,6

Then the PHP side could look like so:
$string = $_GET['arr'];
$string = explode(" ", $string);
echo count($string) . " << Length fo arr";

Hope that helps.

NOTE - I haven't tsted this, so it may not work.
Was This Post Helpful? 1
  • +
  • -

#3 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: passing javascript array to ajax

Posted 08 October 2008 - 05:59 AM

Instead of making your array in Javascript, I would make it in PHP as you can send arrays as strings :)

I would use the following from PHP before you send the array:

$myvar[0] = 'a';
$myvar[1] = 'd';
$myvar[2] = 'g';
//i now have my array
$sendvar = serialize($myvar);//encrypts the array into a string
$sendvar = urlencode($sendvar);//good habit to use urlencode and urldecode but you don't need it in this instance really

//SEND $sendvar TO AJAX
//Javascript code...just put <?=$sendvar;?> in place of your javascript array

Then when you recieve the variable $sendvar in your PHP file (the file that will generate your AJAX output), use:

//This is the file that the AJAX 'gets'
$myarray = urldecode($_GET['sendvar']);
$myarray = unserialize($myarray);

print_r($myarray); // just shows the array

And ye-ho, jobs'a'guddun! :)

This post has been edited by pemcconnell: 08 October 2008 - 06:02 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1