Click to See Complete Forum and Search --> : how to make a chained/contextual select list


thelittlehand
02-28-2005, 05:11 AM
I want to make a pulldown selection menu with two select lists that, when one of the option in the first list is selected, the second list would changed accordingly. Is it easy to do so? or require php/javascript to do so? any information is appreciated. Thank you very much.

phpnovice
02-28-2005, 03:42 PM
Server-side code could be used to build the second drop-down according to what is selected in the first drop-down. The problem is that selecting an option from the first drop-down will not submit the form unless you use JavaScript to do so. The following is one way to do it -- but special handling is required to execute any onsubmit handler you may have defined:

<select name="category1" size="1" onchange="
if (this.form.onsubmit) {
if (this.form.onsubmit()) {
this.form.submit();
}
}">

The other, easier, way to do it is to pass the selection on the URL to the server-side page that will build the second select. The following is an example of that:

<select name="category1" size="1" onchange="
var opt = this.options[this.selectedIndex].value;
self.location.href = self.location.href+'?'+this.name+'='+opt;
">

thelittlehand
02-28-2005, 08:08 PM
thank you phpnovice! It is very handy. You make your point so clear that even code-dummy like me can understand.

phpnovice
02-28-2005, 09:41 PM
Cheers.