Jump to content

ajax array help


countdrac

Recommended Posts

on a listbox change i need populate a new listbox getting info from a DB

 

i can get all the info but im sitting with an array in my php form... can someone please explain what the best way to handle an array from the php and then put those into the new listbox...

 

i was thinking i could just return (from php) one long string with a seperator (eg commas) - then i can get each one individually in the JS (when populating the listbox)

 

but there must be an easier way?

 

this is kinda what im having trouble with...

 

while ($row = mysql_fetch_array($result))
{
	$list[$i][0] = $row['title'];
	$list[$i][1] = $row['file_name'];
                $i++; 
}


mysql_close($connection);
echo $listArray; //dont really know how to return this properly as an array

So this is what i was doing before returning the array... should i keep this one and change the way php returns (string instead of array)?

                var response = xmlHttp.responseText;
	var plist = response.split(",");
	var pfile = "";
	var length = plist.length;

	for (var i=0; i < length-1; i++)
	{
	    var parshaEl = document.createElement('option'); //create option 
      	    var El = document.getElementById('list'); 
	    El.appendChild(parshaEl); //then append it to the second dropdown list 
	    pfile = plist[i].split("##"); //when using the string the string was like title1##filename1,title2##filename2,...
	    pfile[1]+=".inc";
	    parshaEl.value = pfile[1]; 
	    parshaEl.innerHTML = pfile[0];

 

thanks

Link to comment
Share on other sites

I used to serialize it using the php serialize function and unserialize it with a javascript unserialize function(this is not built in you'll have to download it from the net basically when you unserialize it, it will become a javascript array,

 

you can check out more on serializing over here http://www.phpguru.org/static/PHP_Unserialize.html

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.