You don't need to reload the page to get the second dropdown to work. You can change the contents of a dropdown on-the-fly with JavaScript.
The problem with your refresh approach is that your page will need the second dropdown populated in the HTML. You can do this with PHP.
I'll summarize both ways of doing it:
Completely client side -- one request:
PHP generates the javascript code to automatically change the options list of the second dropdown. PHP needs to know ahead of time what ALL the options are, so that it can generate the proper javascript code to populate the options arrays.
Once the page is loaded, and the user changes something from the first dropdown, the onChange event runs a javascript function that changes the options list of the second dropdown automatically. No further requests of the server are made.
[b]
Client/Server -- multiple requests:
The OnChange event of the first dropdown submits a form with the value of the first dropdown. PHP gets this value and re-generates the HTML page, populating the second dropdown with the values corresponding to the submitted value.
I'll give a brief example of both versions. Suppose you had two lists, colors and fruits. The "Red" fruits are "Apple", "Tomato", and "Pomegranate". The "Green" fruits are "Kiwi" and "Grape". The only "Yellow" fruit is "Banana".
When the user selects a color from the first dropdown, the second dropdown is populated with the fruits corresponding to the color selected.
In the first model, you'd have PHP generate ALL the javascript code required to change the second dropdown without making any new requests of the server. Here is the javascript code that I'd use to do this.
// This is a handy function that empties a select dropdown's options
function clearOptions(selectObject)
{
if(document.layers)
{
selectObject.options.length = 0;
}
else
{
selectObject.innerHTML = "";
}
}
function opt(value, text)
{
this.text = text;
this.value = value;
}
var fruits = new Array();
fruits["Red"] = new Array();
fruits["Green"] = new Array();
fruits["Yellow"] = new Array();
fruits["Red"].push(new opt("Apple", "Apple"));
fruits["Red"].push(new opt("Tomato", "Tomato"));
fruits["Red"].push(new opt("Pomegranate", "Pomegranate"));
fruits["Green"].push(new opt("Kiwi", "Kiwi"));
fruits["Green"].push(new opt("Grape", "Grape"));
fruits["Yellow"].push(new opt("Banana", "Banana"));
// This is the function that will actually change the options list
// of the second dropdown based on the selected value of the first.
function sync_fruits(colorSelect, fruitSelect)
{
var id = colorSelect[colorSelect.selectedIndex].value;
var name = colorSelect[colorSelect.selectedIndex].text;
clearOptions(fruitSelect);
if (id == 0)
{
fruitSelect.options[0] = new Option('Select a color', 0);
fruitSelect.disabled = true;
fruitSelect.size = 1;
}
else
{
var colorFruits = fruits[id];
for (var i in colorFruits)
{
fruitSelect.options[fruitSelect.options.length] = new Option(colorFruits[i].text, colorFruits[i].value);
}
fruitSelect.disabled = false;
}
}
In your first <select> field, set the onChange to
onChange="javascript
:sync_fruits(this, document.<form name>.<fruit select name>);"
where <form name> is the name of your form, and <fruit select name> is the name of your 2nd select input field. For example:
<form name="my_form" method="get" action="whatever.php">
Select a color:
<select name="color_field"
onChange="javascript
:sync_fruits(this, document.my_form.fruit_field);">
<option value="0">Select a color</option>
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Yellow">Yellow</option>
</select>
Select a fruit:
<select name="fruit_field" disabled="true">
<option value="NULL">Select a color first</option>
</select>
</form>
Remember -- the code above is what you'll output from PHP. For example:
<?php
// generate the javascript array initializations
$query = "SELECT color, fruit FROM fruit_table";
$result = mysql_query($query);
$fruits = array();
while($row = mysql_fetch_array($result))
{
$fruits[$row['color']][] = $row['fruit'];
}
/* The above code will create a nested array of this form:
$fruits: Array
(
[Red] => Array
(
[0] => Apple
[1] => Tomato
[2] => Pomegranate
)
[Green] => Array
(
[0] => Kiwi
[1] => Grape
)
[Yellow] => Array
(
[0] => Banana
)
)
*/
echo "var fruits = new Array();\n";
echo "\n";
// output color declarations first
foreach($fruits as $color => $cfruits)
{
echo "fruits[\"{$color}\"] = new Array();\n";
}
// now populate each fruit
foreach($fruits as $color => $cfruits)
{
echo "\n";
foreach($cfruits as $fruit)
{
echo "fruits[\"{$color}\"].push(new opt(\"{$fruit}\", \"{$fruit}\"));\n";
}
}
?>
For the client-server model, you won't have any additional javascript code outside of your onChange event. The onChange event should just submit the form. The PHP page receiving the form must then determine what needs to happen next -- Is the form being submitted because the first dropdown selection changed, or is the form being submitted because the user filled out all the fields?
You can use a hidden form field to make things easier on the PHP side. The value of the hidden field will be something like "User Submit" by default. The onChange of a select dropdown can change this value to something to let the PHP script know that the form was submitted as the result of an onChange. In our example, we can set this value to be "Color Change".
<form name="my_form" method="post" action="my_form.php">
<input type="hidden" name="why_submit" value="User Submit" />
<select name="color_field"
onChange="javascript
:document.myform.why_submi t.value='Color Change';document.my_form.submit();">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Yellow">Yellow</option>
</select>
<select name="fruit_field" disabled="true">
<option value="0">Select a color first.</option>
</select>
</form>
The above HTML form would be generated by your PHP script. Your PHP script should check for the existence of $_POST so it can pre-populate the form fields with any data the user already submitted. If $_POST['color_field'] is set, then you can generate the appropriate selections for the fruit_field as well.
<?php
// Generate the fruit options for a specified color:
if (isset($_POST['color_field']))
{
$query = "SELECT fruit FROM fruits WHERE color='{$_POST['color_field']}'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
echo " <option value=\"{$row['color']}\">{$row['color']}</option>\n";
}
}
Keep in mind that none of this code was tested -- I typed it all into the reply textarea directly, so YMMV. Also, there's a lot of code missing from these examples, but that's intentional -- you should have enough code to start with and enough description to figure out the rest and fill in the blanks.
Figuring the rest out for yourself will be a lot more valuable to you than just getting a completely working solution.
Take care,
Nik
http://www.bigaction.org/