SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    keep selected data in dropdown after submit

    In my form, user selects date from dropdown that is data pulled from sql. User then hits a submit button and data is returned in a table based on that date chosen from the dropdown. As it works now the date is reset in the dropdown to the initial date. I want the dropdown to keep the date chosen. Is this possible, and how do I go about it?

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,029
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    I do this quite often but it's easier if you create your drop-down programatically in an ASP loop. That way you can save the selected value into a variable and then in the loop that you are using to create your drop-down you can use a conditional to check if it is the current value and if so add selected = "selected".

    Here's an example that creates a dropdown list of Canadian Provinces and US States. The user will have submitted a form at some point which will have set a variable called "province" into a session.... ie: session("province") = Request("province"):

    Code:
    <%    
        provarray  = array("AB", "BC", "MB", "NB", "NL", "NS", "NT", "NU", "ON", "PE", "QC", "SK", "YT")
        statearray = array("AK", "AL", "AR", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "HI", "IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN", "MO", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VA", "VT", "WA", "WI", "WV", "WY", "--International--")
    %>
        <select name="province" class="frm" id="province">
        <option value="">Province/State</option>
        <option value="">--Canada--</option>
        <%    
            limit = UBOUND(provarray)
            for x=0 to limit
                if session("province") = "" AND provarray(x) = "BC" then
                    response.write "<option value=""" & provarray(x) & """ selected=""selected"">" & provarray(x) & "</option>"
                else
                    if session("province") = provarray(x) then
                        response.write "<option value=""" & provarray(x) & """ selected=""selected"">" & provarray(x) & "</option>"
                    else
                        response.write "<option value=""" & provarray(x) & """>" & provarray(x) & "</option>"
                    end if
                end if
            next    
        %>
        <option value="">--US--</option>
        <%    
            limit = UBOUND(statearray)
            for x=0 to limit
                if session("province") = statearray(x) then
                    response.write "<option value=""" & statearray(x) & """ selected=""selected"">" & statearray(x) & "</option>"
                else
                    response.write "<option value=""" & statearray(x) & """>" & statearray(x) & "</option>"
                end if
            next    
        %>
        </select>
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development


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
  •