SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    PHP Otaku Gibb's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    My dropdown menu won't set to a specific value

    I could of sworn I had this working at some point a long time ago, but I'm at a loss.

    Basically, I'm trying to repopulate my form fields in the event that the user enters invalid data, or no results are found, etc. I have a couple of drop down menus that I want to reset to whatever the user chose before they submitted the form.

    I tried this:

    <select name="state" value="<%= SearchState %>">
    <option value=""></option>
    <option value="al">Alabama</option>
    <option value="tx">Texas</option>
    </select>

    and even this:

    <select name="state" value="tx">
    <option value=""></option>
    <option value="al">Alabama</option>
    <option value="tx">Texas</option>
    </select>

    I tried searching around on this forum and elsewhere, but I can't find a clear answer to how to set a drop down menu to one of the choices.

    Any advice would be greatly appreciated.
    -Gibb

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, that's not how you do it. To select 'Alabama':
    HTML Code:
    <select name="state">
      <option value=""></option>
      <option value="al" selected>Alabama</option>
      <option value="tx">Texas</option>
    </select>
    Programattically you'll have to have an 'if' statement for each option to see which one should have the selected attribute. It's a pain.
    Birnam wood is come to Dunsinane

  3. #3
    PHP Otaku Gibb's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AutisticCuckoo View Post
    No, that's not how you do it. To select 'Alabama':
    HTML Code:
    <select name="state">
      <option value=""></option>
      <option value="al" selected>Alabama</option>
      <option value="tx">Texas</option>
    </select>
    Programattically you'll have to have an 'if' statement for each option to see which one should have the selected attribute. It's a pain.
    Doh, that's what I was afraid of. I didn't write the original code, so they have 65 lines of states and provinces that I'd have to write if statements around. Maybe I'll finally put the states into a database like I was planning a while back.

    Thanks for the reply

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heh ... I recently had to do it with a list of 290 towns and cities.

    Use a function to do it:
    Code:
    Sub state(value, name, search)
        Dim sel : sel = ""
        If value = search Then sel = " selected"
        Response.Write "<option value=""" & value & """" & selected & ">" & name & "</option>" & vbCrLf
    End Sub
    HTML Code:
    <select name="state">
    <&#37;
      state "", "", SearchState
      state "al", "Alabama", SearchState
      state "tx", "Texas", SearchState
    %>
    </select>
    Birnam wood is come to Dunsinane

  5. #5
    PHP Otaku Gibb's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah I crammed everything into a database so I only need 1 if statement and a for or while loop now. It's working pretty good, thanks for the response .


Bookmarks

Posting Permissions

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