...

View Full Version : changing the "selected" option in select box based on referred page



AshleyQuick
07-20-2004, 06:42 PM
<select size="1" name="Fruits" id="choice">
<option value="Apples" selected>Apples</option>
<option value="Bananas">Bananas</option>
<option value="Grapes">Grapes</option>
<option value="Oranges">Oranges</option>
<option value="Strawberries">Strawberries</option>
</select>

Is there a way to have "Grapes" selected if coming from a specified page...say grapes.asp?

Ash

oracleguy
07-20-2004, 06:52 PM
Yep.



<select size="1" name="Fruits" id="choice">
<option value="Apples" selected>Apples</option>
<option value="Bananas">Bananas</option>
<option value="Grapes"

<% if Instr(LCase(Request.ServerVariables("HTTP_REFERER")), "grapes.asp")<>0 Then %>

selected="1"
<% End If %>
>Grapes</option>
<option value="Oranges">Oranges</option>
<option value="Strawberries">Strawberries</option>
</select>

angst
07-20-2004, 06:53 PM
could use something like this:



<select size="1" name="Fruits" id="choice">
<option value="Apples" <% if request("Fruits")="Apples" then response.write "selected" end if%>>Apples</option>
<option value="Bananas" <% if request("Fruits")="Bananas" then response.write "selected" end if%>>Bananas</option>
<option value="Grapes" <% if request("Fruits")="Grapes" then response.write "selected" end if%>>Grapes</option>
<option value="Oranges" <% if request("Fruits")="Oranges" then response.write "selected" end if%>>Oranges</option>
<option value="Strawberries" <% if request("Fruits")="Strawberries" then response.write "selected" end if%>>Strawberries</option>
</select>

Ted Varnson
08-28-2004, 03:01 PM
How could I code something that would allow me to have "page.asp?option=Ecology of The Texas Hill Country"

Which would make the fied with that value the selected field?

Morgoth
08-28-2004, 10:39 PM
How could I code something that would allow me to have "page.asp?option=Ecology of The Texas Hill Country"

Which would make the fied with that value the selected field?

Well, first off you need to know that in page.asp?option=Ecology
option is a QueryString, and the QueryString equals "Ecology"

So on any page all you have to do is ask for the QueryString.
Variable = Request.QueryString("option")

Some people also have touble with this because this is an unsecure way of transporting variables from one page to the next, but it still is a good way. Just make sure what should be in the QueryString can't be changed to hurt you and your code.

When you send numbers with a QueryString you're numbers are now strings, and much be changed back into integers, or doubles or what ever they were to begin with.
Variable = Int(Request.QueryString("option"))



Page.asp?Apples=selected&Bananas=selected&Grapes=selected&Oranges=selected&Strawberries=selected

To keep it on one line I used code tags.

Now let's read the QueryString:


<select size="1" name="Fruits" id="choice">
<option value="Apples" <%=Request.QueryString("Apples")%>>Apples</option>
<option value="Bananas" <%=Request.QueryString("Bananas")%>>Bananas</option>
<option value="Grapes" <%=Request.QueryString("Grapes")%>>Grapes</option>
<option value="Oranges" <%=Request.QueryString("Oranges")%>>Oranges</option>
<option value="Strawberries" <%=Request.QueryString("Strawberries")%>>Strawberries</option>
</select>


The QueryString doesn't need to be "selected" but this just makes the code smaller, without having to write if statements.

BuddhaMan
08-29-2004, 03:46 AM
Just a side question here.

According to his original post, he's trying to pass a query string with spaces in it. My question is, if a coder need to do that, do you need to convert the spaces to "%20" ?

Is the top or bottom example the way to do it?


<option value="Apples" <%=Request.QueryString("Ecology of The Texas Hill Country")%>>Apples</option>

<option value="Apples" <%=Request.QueryString("Ecology%20of%20The%20Texas%20Hill%20Country")%>>Ecology Dept</option>

oracleguy
08-29-2004, 08:33 PM
Is the top or bottom example the way to do it?


<option value="Apples" <%=Request.QueryString("Ecology of The Texas Hill Country")%>>Apples</option>

<option value="Apples" <%=Request.QueryString("Ecology%20of%20The%20Texas%20Hill%20Country")%>>Ecology Dept</option>

Neither:


<option value="Apples" <%=Request.QueryString("option")%>>Apples</option>

And as for putting values with spaces in, you can just use the Server.URLEncode() method to automatically convert all the characters so that they will go through the URL fine.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum