Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > Client Side Scripting
User Name:
Password:
 

Reply
Thread Tools   Display Modes
  View First Unread
 
Old 09-01-2005, 11:37 PM
  #1
quadoc
Novice (Level 1)
 
Join Date: Sep 2005
Posts: 10
iTrader: (0)
quadoc is an unknown quantity at this point
Question Drop-Down menu on the fly

I've two drop-down menus, but in order for me to populate the 2nd drop down menu, I need to know what item the user selected from the 1st drop down menu.

Has anyone done this before? Please post some tips. Thanks...
quadoc is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-01-2005, 11:45 PM
  #2
blackpepper
Gampi
 
blackpepper's Avatar
 
Join Date: Jul 2005
Location: metro - nyc
Posts: 1,282
iTrader: (0)
blackpepper will become famous soon enough
im not sure if this is possible with html. with php it can be done. itd take me awhile to figure out the coding for something like that so you may want to ask this in the php programming sect of the forum
__________________
blackpepper is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-01-2005, 11:52 PM
  #3
quadoc
Novice (Level 1)
 
Join Date: Sep 2005
Posts: 10
iTrader: (0)
quadoc is an unknown quantity at this point
Cool

CootyHead where are you?
quadoc is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-02-2005, 12:36 AM
  #4
Pegasus
Mama Hen
 
Pegasus's Avatar
 
Join Date: Nov 2001
Location: 35º South of Santa Claus
Posts: 23,240
iTrader: (0)
Pegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to all
You mean like this one?

http://www.a1javascripts.com/form_na...ublecombo.html

Peg
__________________


Decaf is the root of all evil...
HTMLForums Awards 2008
Pegasus is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-02-2005, 01:57 AM
  #5
bendman
Semantic Fanatic
 
bendman's Avatar
 
Join Date: Jun 2004
Location: Washington, DC
Posts: 1,301
iTrader: (0)
bendman is on a distinguished road
php actually couldn't do it unless you refreshed the page or the second drop-down was on a second page, because php doesn't do anything after the browser recieves the file.

I think your best bet would be Peg's example, at least the ones that I've seen before have been in javascript.
__________________
[twitter] [facebook]
bendman is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-02-2005, 07:07 AM
  #6
quadoc
Novice (Level 1)
 
Join Date: Sep 2005
Posts: 10
iTrader: (0)
quadoc is an unknown quantity at this point
Any sample in HTML and Java script? Please post some tips. Thanks...
quadoc is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-02-2005, 09:13 AM
  #7
Pegasus
Mama Hen
 
Pegasus's Avatar
 
Join Date: Nov 2001
Location: 35º South of Santa Claus
Posts: 23,240
iTrader: (0)
Pegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to allPegasus is a name known to all
Did you check the link I posted? That's in Javascript.

Peg
__________________


Decaf is the root of all evil...
HTMLForums Awards 2008
Pegasus is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-02-2005, 10:08 AM
  #8
quadoc
Novice (Level 1)
 
Join Date: Sep 2005
Posts: 10
iTrader: (0)
quadoc is an unknown quantity at this point
yes, I downloaded it. It pretty good, but still not what I wanted. What I'm trying to do is get the user selection from the first drop-down box then based on the selected item, populate the 2nd drop-down box. How do I get the returned select value of the first drop-down box?

Here is my code in pseudo-code

1 - Display 1st drop-down box (two items in the box item#1, item #2)

2 - If user selected item #1 then
SELECT * FROM CATEGORY with DEPT = "item #1"
If user selected item #2 then
SELECT * FROM CATEGORY with DEPT = "item #1"

3 - Populate 2nd drop-down box with the select results.

Could someone show me some tips on doing this? Thanks...
quadoc is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-02-2005, 11:03 AM
  #9
awmt102
Novice (Level 1)
 
Join Date: Aug 2005
Posts: 7
iTrader: (0)
awmt102 is an unknown quantity at this point
I had exactly the same problem and i found a way to do it:

first of all you need to declare these three functions:

function populateBox(oBox, nIndex, aArray){

for (var i = 0; i < aArray[nIndex].length; i = i + 1){

oBox.options[oBox.options.length] =
new Option(aArray[nIndex][i], aArray[nIndex][i]);
}
}

function clickcombo1(elem,array1){
clearcombo(document.f1.combo2);

populateBox(document.f1.combo2,
elem[elem.selectedIndex].value, array1);
return true;
}

function clearcombo(elem){
var i;
for (i = elem.options.length; i >= 0; i--) elem.options[i] = null;
elem.selectedIndex = -1;
}


You then put in your form as required. Mine looked something like this:

<select NAME="combo1" SIZE="5" ONCHANGE="clickcombo1(this,ArrayToPopulateSecondList)"></select>

<select NAME="combo2" SIZE="5"></select>

Further in the file you must also include this line:

populateBox(f1.combo1,'0',ArrayToPopulateFirstBox);

Where f1 is the name of the form that combo1 appears in.

At this point i will describe how this actually works:

Each select box is populated by a javascript array, i.e. each element in the array is a different option in the box. The second box is populated using another array, although this time it is a two dimensional one. the first dimension is exactly the same as the array that populates the first box. The second dimension contains the data you wish to appear in the second box when the first dimension's value is the same as the first select boxes value.

That is prob quite confusing so here is a simple example (in fact it is exactly the example i have used on my site).

There are a number of text files available on my site. They are made up of different languages, some french, some english, some spanish etc. The idea is that you select the language you wish to view download these particular files in, and the second box is populated with only the files in this language. (The language<->File info is held in a database).

So the first array is simply an array of the languages available:

array[0]=English
array[1]=French
array[2]=Spanish

The second array is 2 dimensional:

array2[English][0]=FirstEnglishFile
array2[English][1]=SecondEnglishFile
array2[French][0]=FirstFrenchFile
array2[French][1]=SecondFrenchFile
array2[French][2]=ThirdFrenchFile
array2[Spanish][0]=OnlySpanishFile

So now, when you select English, the second box will show:

FirstEnglishFile
SecondEnglishFile

You must note that the first array needs only be one dimensional to hold the data, however, if you try and pass a one-dimensional array into the function populatebox() it will complain. This is because it is designed to cope with populating the second box as well, which uses a 2D array. For this reason the first array must also be 2D, but only one of the dimensions is actually used. i.e the first array will be:

array[0][0]=English
array[0][1]=French
array[0][2]=Spanish


So thats how its done in javascript and html, if you have any probs understnading then let me know, however it looks like u are using php (as i did). This causes some small hassle because php arrays are different from javascript arrays. I will explain in another post (in just a few minutes) how to do that.
awmt102 is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-02-2005, 11:25 AM
  #10
awmt102
Novice (Level 1)
 
Join Date: Aug 2005
Posts: 7
iTrader: (0)
awmt102 is an unknown quantity at this point
as i already mentioned the arrays you use must be javascript arrays. So if you are using php then you need to convert any arrays to javascript:

<?php
function PhpArrayToJsArray($array, $arrayName)
{
return 'var ' . $arrayName . ' = ' . PhpArrayToJsArray_Recurse($array, $arrayName);
}

function PhpArrayToJsArray_Recurse($array, $arrayName)
{
// Base case of recursion
if(! is_array($array) )
{
return '"' . $array . '";' . "\n";
}

$retVal = "new Array();\n";
foreach($array as $key => $value)
{
$key = '"' . $key . '"';
$retVal .= $arrayName . '[' . $key . '] = ' . PhpArrayToJsArray_Recurse($value, $arrayName . '[' . $key . ']');
}

return $retVal;
}
?>

This function will convert ANY php array to javascript, i.e. any number of dimensions. It returns a string of javascript that you must run to create the javascript array.

Its usage is as follows:

<?php
$JavascriptToRun=PhpArrayToJsArray($phpArray,"JavascriptArray");

echo "
<script type=\"text/javascript">
<!--
$JavascriptToRun
//-->
</script>
";
?>

You now have a javascript copy of your php array.

Obviously you need to populate your php arrays from your database first. Mine looked similar to this:

<?php
$LanguageArray["0"]=array();
$QueryResult=mysql_query(SELECT * FROM Files);
while($row=mysql_fetch_row($QueryResult);
{
if(!in_array($row[3],LanguageArray["0"]))//if its not already in the array
{
$LanguageArray["0"][$i]=$row[3];//add the language to the list
$i++;//increase $i
$LanguageCount[$row[3]]++;//increse the count of the number of files in this language
}
$FileArray[$row[3]][$LanguageCount[$row[3]]]=$row[4];//enter the file info
?>

The $LanguageCount array stores the number of files that have been processed of a particular language so that they can be put at the end of the $FileArray[$Language] array. (there are doubtless other ways to do this).

Hope all this helps - it took me ages to figure it all out form various posts i found on the net, so if you have any problems let me know

good luck
awmt102 is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-02-2005, 03:43 PM
  #11
quadoc
Novice (Level 1)
 
Join Date: Sep 2005
Posts: 10
iTrader: (0)
quadoc is an unknown quantity at this point
Thumbs up

Wow, those are some seirious coding. Let me sleep over it tonite. Thanks for the tips.
quadoc is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-02-2005, 04:28 PM
  #12
awmt102
Novice (Level 1)
 
Join Date: Aug 2005
Posts: 7
iTrader: (0)
awmt102 is an unknown quantity at this point
There is a lot of code there. a lot of the javascript was not actually written by me, but i understand it. I started programming with C so a lot of the php is quite indepth. As i say, if you dont understand any of it just let me know and i will endeavour to explain.
awmt102 is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 05-18-2006, 01:59 PM
  #13
therendezvous
Novice (Level 1)
 
Join Date: May 2006
Posts: 1
iTrader: (0)
therendezvous is an unknown quantity at this point
Exclamation

hi
I've never really written javascript before but just had few queries about the javascript you(awtm2) have written.

1) where should we included the populateBox() function in the file n how?
2) and what could be done to have a cyclick drop-down menu, for instance

select dropDown1 --> change dropDown2
change dropDown2 --> change dropDown1

thanks.
therendezvous is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Reply
KEEP TABS
SPONSORS
 
Boxedart

 
 


 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
  
 
 
 



 
  POSTING RULES
 
 
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 05:48 AM.

   

Mascot team created by Drawshop.com

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.

Server Monitoring by ENIACmonitor 0.01
HTMLforums.com © Big Resources, Inc. Web Design by BoxedArt.com
vRewrite 1.5 beta SEOed URLs completed by Tech Help Forum and Chalo Na.