Forum Moderators: open

Message Too Old, No Replies

Javascript To Reset The Value Of A Select List?

         

tekneck

9:06 pm on Nov 10, 2009 (gmt 0)

10+ Year Member



I have three select lists that build a search on my site. They are dependent in the PHP back end script. When a user selects from the first select list, the page refreshes (onChange submit) and the second select list is refreshed with related options. When the user selects an option from the second select list, the page is reloaded with the search result.

The problem is that when someone decides to change the first option list value, the second select option needs to be reset to its default value...

So if user selects Category 1 the script queries and returns results. If the user selects Sub-Category 2, the script now narrows down the results and returns the results. BUT... if the user selects Category 2, the Sub-Category selection remains and there are no results returned. I want to reset the sub-category select list to 'any' when the user changes the first select.

What is the best way to do this? I thought maybe moving totally to chained selects but this way works except for this one issue that one of you might be able to help me with...

Alternatively, can anyone suggest a good chained select script for PHP where the select lists are built from my MYSQL queries (which I have already built).

Thanks gurus...

Here is the code:

<form action="" method="get">

<select name="cat" onChange="this.form.submit();">
<option value="any" selected>Any</option>
<option value = "1">Category 1</option>
<option value = "2">Category 2</option>
</select>

<select name="subcat" onChange="this.form.submit();">
<option value="any" selected>Any</option>
<option value = "sub1">Sub-Category 1</option>
<option value = "sub2">Sub-Category 2</option>
</select>

<input name="Search" type="submit" />
</form>

Fotiman

9:35 pm on Nov 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld!
Does this work?
<select name="cat" onChange="this.form.subcat.selectedIndex=0;this.form.submit();">

tekneck

3:43 am on Nov 11, 2009 (gmt 0)

10+ Year Member



Yes... perfectly. Thank you. How simple can it get!