Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    308
    Thanks
    0
    Thanked 0 Times in 0 Posts

    changing the "selected" option in select box based on referred page

    <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

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Yep.

    Code:
    <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>
    OracleGuy

  • #3
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    could use something like this:

    Code:
    <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>

  • #4
    New Coder
    Join Date
    Apr 2004
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #5
    Senior Coder Morgoth's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada Remaining Brain Cells: 6
    Posts
    1,402
    Thanks
    2
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Ted Varnson
    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"))

    Code:
    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:
    Code:
    <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.

  • #6
    New Coder
    Join Date
    Aug 2004
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

    Code:
    <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>

  • #7
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by BuddhaMan
    Is the top or bottom example the way to do it?

    Code:
    <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:

    Code:
    <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.
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •