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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if a select box has been selected

Status
Not open for further replies.

PushCode

Programmer
Dec 17, 2003
573
US
I have a page with multiple select boxes that the user will use to refine the results on the page. The form uses method="post".

How do I make sure a select box has been selected?
Here's an example of a select box:
<select name="category">
<option value="">All</option>
<option value="23">Category Name</option>
</select>

Here's an example of what I'm trying to do in ASP:
If len(Request.Form("category")) > 1 Then
Do This
End If

It's not working. I get a Syntax Error. Any help?
 
checkboxes arent included in the request object if they are not checked...

so :
if request("cbox") <> "" then
...

tells you that something was selected.
 
What is the syntax error you are receiving? Your code looks fine from here.

 
I figured it out. I was doing the If Then inside of some SQL query code, and I think I was handling that improperly.

On another note, how do you ensure that on a page refresh, that the selected items in select boxes remain selected.

Say I've got the following dynamic select box:

Code:
<select name="selSize" class="frm_select">
	<option value="">[ All ]</option>
	<%
	do until rsSize.eof
	%>
	<OPTION value="<%= rsSize("size_id")%>"><% = rsSize("t_size")%></OPTION>
	<%
	rsSize.movenext
	loop
	set rsSize = nothing
	%>
</select>

How do I maintain the selected state of whichever option is selected? Thanks!
 
You can just do an if/then statement to append the selected property of your options. Going from memory here, so this may need to be modified.
Code:
<select name="selSize" class="frm_select">
    <option value="">[ All ]</option>
    <%
    do until rsSize.eof
    %>
    <OPTION [COLOR=red]<% if request.form("category") = rsSize("size_id") then %>selected<% end if %>[/color] value="<%= rsSize("size_id")%>"><% = rsSize("t_size")%></OPTION>
    <%
    rsSize.movenext
    loop
    set rsSize = nothing
    %>
</select>

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
I tried your suggestion but it doesn't seem to be working.
No errors or anything, id just displays the default(first) [ All ] option.
Here's what I have:
Code:
<select name="selSize" class="frm_select">
			<option value="">[ All ]</option>
			<%
			do until rsSize.eof
			%>
			<OPTION<% if request.form("selSize") = rsSize("size_id") then %>selected<% end if %> value="<%= rsSize("size_id")%>"><% = rsSize("t_size")%></OPTION>
			<%
			rsSize.movenext
			loop
			set rsSize = nothing
			%>
		  </select>
 
Do a response.write of your request.form("selSize") variable and then compare that to what is in your DB for the "size_id" value. If they're different, then your problem is solved by making sure you get the right variable. Otherwise, check your syntax (I did mention that you may need to modify it as I was going from memory).

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
I did a response.write of request.form("selSize") and it is the correct number.

Can anyone see why this isn't working?
 
It's probably just a syntax issue. Try to put a space between Option and Selected.
Code:
<OPTION<% if request.form("selSize") = rsSize("size_id") then %> selected <% end if %> value="<%= rsSize("size_id")%>"><% = rsSize("t_size")%></OPTION>

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
I had already noticed and tried that to no avail. Any other ideas?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top