Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use onChange event handler and dropdown listbox

Status
Not open for further replies.

Ken011

MIS
Oct 13, 2006
66
US
Hello experts,

I have a dynamic dropdown listbox that get automatically populated by querying the database.

Users then select an option from the dynamically populated dropdown listbox to perform a search.

For instance,

<select name="test">
<option value="">All tests</option>
<option>test1</option>
<option>test2</option>
<option>test3</option>
</select>

As it stands now, the user has to select one of the options, say test1 for instance, and then click Search.

Then the result is displayed in results page.

What I would like to do is put an onChange event handler into this dropdown so that anytime a user selects an option, say test1 for instance, s/he is automatically taken to the results page where the result is displayed, without the need to click on the Search button.

I tried this:

<select name="test" onchange="this.form.submit();">

However, when I select an option, it displays the first row of records from the db but won't select any other option.


Here is one of the dropdown code:

prefix = "<option>"
suffix = "</option>" & vbNewLine

Set RS = Conn.Execute("SELECT DISTINCT Test FROM testTable ORDER BY test")
temp = RS.GetString( , , , suffix & prefix )
OptionsTests = prefix & Left( temp, Len(temp)-Len(prefix) )

Then the dropdown:
<select name="test" onchange="this.form.submit();">
<option value="">All tests</option>
<%=OptionsTests%>
</select><br>

There is got to be something that refreshes the dropdown and ready it for next option selection.


Thanks very in advance for your assistance
 
Check the source generated by
Code:
<select name="test" onchange="this.form.submit();">
<option value="">All tests</option>
<%=OptionsTests%>
</select><br>
Are you missing a terminating </option> or do you have an unterminated <option>? I don't know how to use rs that well. I would have done the simpleton's way i.e.
Code:
OptionsTests = ""
do while not RS.eof
   OptionsTests = OptionsTests & prefix & RS("Test") & suffix
   RS.MoveNext
loop
 
Just looked up getString. What you're doing looks right but I'd still check the generated code. You might be off by 1 somewhere.
 
>However, when I select an option, it displays the first row of records from the db but won't select any other option.
One reason which might result in this is that your select-one element does not have the value scripted explicitly. Now, in that case, you are at the mercy of the particular implementation of the browser which might elect the text property at the place of the value attribute when submitting. For ie, it might submit the "empty".

You have the explicitly set the value attribute.
[tt]
<select name="test" onchange="this.form.submit();">
<option value="">All tests</option>
<%
Test=request.form("test")
if Test<>"" then
Set RS = Conn.Execute("SELECT DISTINCT Test FROM testTable ORDER BY test")
if not RS.eof then
response.write "<optin value='" & RS("test").value & "'>" & RS("test").value & "</option>" & vbcrlf
</select><br>
RS.movenext
end if
end if
%>
</select>
[/tt]
Same in the original page (where you submit the request.form("test")) or the response page.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top