Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2004
    Posts
    22

    Unanswered: Pre-select a menu item from a cookie

    I have used a cookie to store the value of a previously submitted select menu.

    The script reads the cookie and attempts to pre-select the menu item based upon the cookie value.

    Except it doesn't work for me and I cannot work out why.


    <%
    If Request.Cookies ("SamplesInBatch")("Batch") <> "" Then
    BatchSelected = Request.Cookies ("SamplesInBatch")("Batch")
    Else
    BatchSelected = 0
    End If
    %>

    <select name="Batch" id="Batch">
    <% Dim i
    strBatchSelected = ""
    For i=0 to 5
    IF i = BatchSelected Then
    'Response.Write(BatchSelected)
    strBatchSelected = "selected"
    End If
    response.Write("<option " & strBatchSelected & " value=""" & i & """>" & i & "</option>" & "")
    Next
    %>
    </select>



    If the value of the cookie ("SamplesInBatch")("Batch") is 3, then I would like to get the following:



    <select name="Batch" id="Batch">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option selected value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    </select>



    Any ideas where I am going wrong? What's strange is that if I change the line:

    'IF i = BatchSelected Then' to

    'IF i = 3 Then' - I get some very strange results and menu item 3 and above gets the 'selected' treatment to give:


    <select name="Batch" id="Batch">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option selected value="3">3</option>
    <option selected value="4">4</option>
    <option selected value="5">5</option>
    </select>


    Cheers,
    Alski

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Code:
    <select name="Batch" id="Batch">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option selected="selected" value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    </select>
    George
    Home | Blog

  3. #3
    Join Date
    Jan 2004
    Posts
    22
    Have I missed something? Which line(s) of code should I change?

  4. #4
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    In FF the bold text stands out, but not in ruddy IE!
    Code:
    <option selected="selected" value="3">3</option>
    George
    Home | Blog

  5. #5
    Join Date
    Jan 2004
    Posts
    22
    I cheated/gave up on the For statement as I was getting odd results - but the following works fine:

    <select name="Batch" id="Batch">
    <option <%If BatchSelected = 0 Then Response.Write("selected=""selected""") End If%> value="0">0</option>
    <option <%If BatchSelected = 1 Then Response.Write("selected=""selected""") End If%> value="1">1</option>
    <option <%If BatchSelected = 2 Then Response.Write("selected=""selected""") End If%> value="2">2</option>
    <option <%If BatchSelected = 3 Then Response.Write("selected=""selected""") End If%> value="3">3</option>
    <option <%If BatchSelected = 4 Then Response.Write("selected=""selected""") End If%> value="4">4</option>
    <option <%If BatchSelected = 5 Then Response.Write("selected=""selected""") End If%> value="5">5</option>
    <option <%If BatchSelected = 6 Then Response.Write("selected=""selected""") End If%> value="6">6</option>

    </select>

  6. #6
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Code:
    <select name="Batch" id="Batch">
        <%
            Dim i
            strBatchSelected = ""
    
            For i=0 to 5
        %>
        <option value='<%= i %>'
            <%
                If i = BatchSelected Then
                    Response.Write " selected='selected' "
                End If
            %>
        >
            <%= i %>
        </option>
        <%
            Next i
        %>
    </select>
    George
    Home | Blog

Posting Permissions

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