Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

populate list box from php array stored as javascript variable 1

Status
Not open for further replies.

jmoore2141

Programmer
May 18, 2007
6
US
I'm using ajax to return an array based on a value selected from list box1. The objective is then to clear listbox2 and repopulate with the array. I am successfully returning the php array and storing as a javascript variable and than clearing listbox 2. In the next step I'm loading the list box. The problem is I'm unable to strip out the php array structure. Any ideas on how to approach this would be appreciated.

options in list box look like:

array(2){[0]=> string(14)
Some Value1
[1]=>string(21)
Some Value2
[2]=>string(21).....


The array being passed into script looks like:

array(2){
[0]=>
string(14)"SomeValue1"
[1]=>
string(21)"SomeValue2"
}



Current code to re-populate listbox

// Repopulate the list box
var selbox = document.Form1.customer;
var al = content;
var alsplit = al.split('"');
for(i=0;i<alsplit.length;i++) {
var sa=alsplit.split('"');
var2 = new Option(sa[0]);
selbox.options[selbox.options.length] = var2;
}
alert(content);
}


Thanks,

Josh
 
Here's an example that will break apart the values in the string that you provided, and store them in an array named "b". This example function will alert each value as it is stored so that you can see that it is storing the correct data:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>title test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

var a = "array(2){\n[0]=>\nstring(14)\"SomeValue1\"\n[1]=>\nstring(21)\"SomeValue2\"\n}";
alert("php return string looks like this:\n\n" + a);
var b = a.split(/\[\d+\]=>/).slice(1);
for (i = 0; i < b.length; i++) {
   b[i] = b[i].substring(b[i].indexOf("\"") + 1, b[i].lastIndexOf("\""));
   alert("b[" + i + "]: " + b[i]);
}

</script>
<style type="text/css"></style>
</head>
<body>

</body>
</html>

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
If I were you jmoore2141, I'd also thank kaht by clicking on the link under his signature that says:

"Thank kaht for this valuable post!"

It looks like he went through a lot of work coming up with his response.


[monkey][snake] <.
 
Why monksnake, how sweet of you to pass on this kind reminder. I don't care what everybody else says about you in this forum, you're OK [thumbsup2]



Seriously though, they should have a tutorial on site mechanics for all new users to let them know how the purple star system works....

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
Hi

jmoore2141, after you clicked the

* [navy]Thank kaht
for this valuable post![/navy]


link, a page had to appear in a pop-up window ( unless you use a overzealous pop-up blocker ) which has a confirmation link. Clicking that link finalizes the rewarding.

Seems you missed that, because there is still no star beside kaht's name. Please repeat the procedure.

Feherke.
 
alas, no purple love for me this time [sad]

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top