Welcome to Dream.In.Code
Getting Help is Easy!

Join 131,476 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,838 people online right now. Registration is fast and FREE... Join Now!




passing javascript array to ajax

 
Reply to this topicStart new topic

passing javascript array to ajax

kummu4help
post 7 Oct, 2008 - 09:49 PM
Post #1


D.I.C Head

Group Icon
Joined: 4 Aug, 2008
Posts: 152



Dream Kudos: 25
My Contributions


HI,
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
CODE

<html>
<head>
<script type="text/javascript">

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

    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null){
        alert ("Browser does not support AJAX - Please update");
        return;
    }
    
    var url="getArray.php";
    url=url+"?arr="+arr;
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);

}

function stateChanged() {
    
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        document.getElementById("show").innerHTML=xmlHttp.responseText;
    }
    
    
}

function GetXmlHttpObject(){
    var xmlHttp=null;
    try{
    // 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;
}

</script>
</head>

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


php code
CODE

<?php
$arr=$_GET['arr'];
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
User is offlineProfile CardPM

Go to the top of the page


BetaWar
post 8 Oct, 2008 - 04:45 AM
Post #2


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,976



Thanked 77 times

Dream Kudos: 1175
My Contributions


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:
CODE
var array = new Array(0, 1,2, 3, 4, 5, 6);
function concat(array){
  var str = "";
  for(i=0; i<array.length; i++){
    if(i>0){
      str += ""+array[i];
    }
    else{
      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:
CODE

<?php
$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.
User is offlineProfile CardPM

Go to the top of the page

pemcconnell
post 8 Oct, 2008 - 04:59 AM
Post #3


D.I.C Regular

Group Icon
Joined: 5 Aug, 2008
Posts: 392



Thanked 35 times

Dream Kudos: 75
My Contributions


Instead of making your array in Javascript, I would make it in PHP as you can send arrays as strings smile.gif

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

CODE

<?php
$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:

CODE

<?php
//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! smile.gif

This post has been edited by pemcconnell: 8 Oct, 2008 - 05:02 AM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/19/08 11:47PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month