Switch to full style
HTML,DHTML,Javascript,XML,CSS
Post a reply

Populating Dropdown..

Tue Jan 27, 2009 8:42 pm

Alrighty..
I have a database that generates this code for a dropdown list (B) based on the selection of a previous dropdown list (A)..

I would like to use this array to populate a single dropdown menu for a different page on page load.

Here is the array the database generates.
---------------------
javascript code
team = new Array(
new Array(new Array(""),
new Array("Automotive Service & Repair/Oil Change"),
new Array("Car Wash/Detailing Service"),
new Array("Tire Stores"),
new Array("Transmission Centers")
),
new Array("")

),

----------------------

below is how the full script on the original (2 dropdown) page.
-----------------------
javascript code
<script langauge="javascript">
team = new Array(
new Array(new Array(""),
new Array("Automotive Service & Repair/Oil Change"),
new Array("Car Wash/Detailing Service"),
new Array("Tire Stores"),
new Array("Transmission Centers")
),
new Array("")

),

function fillSelectFromArray(selectCtrl, itemArray, goodPrompt, badPrompt, defaultItem) {

var i, j;

var prompt;

// empty existing items

for (i = selectCtrl.options.length; i >= 0; i--) {

selectCtrl.options[i] = null;

}

prompt = (itemArray != null) ? goodPrompt : badPrompt;

if (prompt == null) {

j = 0;

}

else {

selectCtrl.options[0] = new Option(prompt);

j = 1;

}

if (itemArray != null) {

// add new items

for (i = 0; i < itemArray.length; i++) {

selectCtrl.options[j] = new Option(itemArray[i][0]);

if (itemArray[i][1] != null) {

selectCtrl.options[j].value = itemArray[i][1];

}

j++;

}

// select first item (prompt) for sub list

selectCtrl.options[0].selected = true;

}

}

// End -->

</script>

------------------------------
So, could the original script be modified to populate a single drop-down?



Re: Populating Dropdown..

Tue Jan 27, 2009 9:25 pm

I tried to understand your code ,your code fill some <select> option right ? based on some condition right ?

But the problem not clear to me :( !!!

Re: Populating Dropdown..

Tue Jan 27, 2009 9:52 pm

Yes.
The original code can be see here..
Code:
http://www.surf2saveinc.com/catlist5.shtml

The Array on that page populates the subcategory list when a selection is made to the category list.

On a new page. I would query the database for a category, and have the "Array" populate a subcategory menu on page load.

I already have the database creating the array, and can include it in the script via SSI, I just don't know how to rewrite the script to populate the <select> options onload.

Re: Populating Dropdown..

Tue Jan 27, 2009 10:10 pm

so you want to choose category then press a button , then you go to a new window where you can see the corresponding subcategories .In that case you will not need javascript .

another case
are you using dynamic web programming language ? sure you are , like jsp ,php, asp.net to make connection to database and get categories . is your problem in filling the <selection> by the result of the query ?

Re: Populating Dropdown..

Wed Jan 28, 2009 12:54 am

Correct! The Problem is filling in the selection...

The database is queried via SSI (http://surf2saveinc.com/cgi-bin/coupons ... Automotive)

That gives us the array...

I need the array to fill in the <select> options in the dropdown menu.

Dazed

Re: Populating Dropdown..

Wed Jan 28, 2009 9:40 pm

Why don't you fill it based on the parameter sent from the category <selection > .

What is SSI ? i saw the link i saw a code for creating array of arrays . I think it is the category list .

Javascript is used to handle data not to fill from database !

Re: Populating Dropdown..

Wed Jan 28, 2009 9:57 pm

SSI = Server Side Include..

I think we've gotten off track, and it probably is my fault. :-)

I'll start over.

I have this array
Code:
new Array(new Array(""), new Array("Cruises"), new Array("Honeymoons"), new Array("Tours/Land Packages")
),


I need it to populate a <select> option list on page load. ie:
<option></option>
<option>Cruises</option>
<option>Honeymoons</option>
<option>Tours/Land Packages</option>

Any ideas?

Re: Populating Dropdown..

Wed Jan 28, 2009 10:16 pm

you can use Onload event .

Re: Populating Dropdown..

Wed Jan 28, 2009 10:21 pm

Any ideas on the script to turn the array to options?

Re: Populating Dropdown..

Thu Jan 29, 2009 12:15 am

I made this code for you :
Code:
<html>
<script type="text/javascript">

  function FillSelection()
  {
 
  var divselection = document.getElementById('divselection');
 
  team = new Array(
  new Array(new Array(""),
  new Array("Automotive Service & Repair/Oil Change"),
  new Array("Car Wash/Detailing Service"),
  new Array("Tire Stores"),
  new Array("Transmission Centers")),new Array("") );
     document.getElementById('subject');
          var myoptions="<select id='myoptions' >";
        alert(team[0].length);
        for(var i=0;i<team[0].length;i++)
        {
           myoptions+="<option>"+team[0][i]+"</option>";
          alert(team[0][i]);
        }
        myoptions+="</select>";
        divselection.innerHTML=myoptions;
  }

</script>
<body onload="javascript: FillSelection();">

<div id="divselection">
<select id="myoptions" >

</select>
</div>





</body>

</html>



Post a reply
  Related Posts  to : Populating Dropdown..
 POPULATING DROPDOWN IN MY DATABASE USING PHP     -  
 Dropdown list in ASP.NET - How to get value selected     -