 |
|
09-01-2005, 11:45 PM
|
|
#2
|
 |
|
Gampi
Join Date: Jul 2005
Location: metro - nyc
Posts: 1,282
|
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
|
|
Add to del.icio.us
Can you digg it?
|
|
|
09-02-2005, 01:57 AM
|
|
#5
|
 |
|
Semantic Fanatic
Join Date: Jun 2004
Location: Washington, DC
Posts: 1,301
|
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.
|
|
Add to del.icio.us
Can you digg it?
|
|
|
09-02-2005, 07:07 AM
|
|
#6
|
 |
|
Novice (Level 1)
Join Date: Sep 2005
Posts: 10
|
Any sample in HTML and Java script? Please post some tips. Thanks...
|
|
Add to del.icio.us
Can you digg it?
|
|
|
09-02-2005, 09:13 AM
|
|
#7
|
 |
|
Mama Hen
Join Date: Nov 2001
Location: 35º South of Santa Claus
Posts: 23,240
|
Did you check the link I posted? That's in Javascript.
Peg
|
|
Add to del.icio.us
Can you digg it?
|
|
|
09-02-2005, 10:08 AM
|
|
#8
|
 |
|
Novice (Level 1)
Join Date: Sep 2005
Posts: 10
|
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...
|
|
Add to del.icio.us
Can you digg it?
|
|
|
09-02-2005, 11:03 AM
|
|
#9
|
 |
|
Novice (Level 1)
Join Date: Aug 2005
Posts: 7
|
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.
|
|
Add to del.icio.us
Can you digg it?
|
|
|
09-02-2005, 11:25 AM
|
|
#10
|
 |
|
Novice (Level 1)
Join Date: Aug 2005
Posts: 7
|
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
|
|
Add to del.icio.us
Can you digg it?
|
|
|
09-02-2005, 03:43 PM
|
|
#11
|
 |
|
Novice (Level 1)
Join Date: Sep 2005
Posts: 10
|
Wow, those are some seirious coding. Let me sleep over it tonite. Thanks for the tips.
|
|
Add to del.icio.us
Can you digg it?
|
|
|
09-02-2005, 04:28 PM
|
|
#12
|
 |
|
Novice (Level 1)
Join Date: Aug 2005
Posts: 7
|
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.
|
|
Add to del.icio.us
Can you digg it?
|
|
|
05-18-2006, 01:59 PM
|
|
#13
|
 |
|
Novice (Level 1)
Join Date: May 2006
Posts: 1
|
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.
|
|
Add to del.icio.us
Can you digg it?
|
|
|
KEEP TABS |
|
SPONSORS |
| |

|
| |
|
|
| |
|