Wrox Programmer Forums
|
ASP Forms As of Oct 5, 2005, this forum is now locked. Please use "Classic ASP beginner" at http://p2p.wrox.com/forum.asp?FORUM_ID=54 or "Classic ASP Professional" http://p2p.wrox.com/forum.asp?FORUM_ID=56 instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP Forms section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 13th, 2004, 11:18 AM
Authorized User
 
Join Date: Jan 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default saving values from drop down lists

Hi

How do I save a value from a drop down list for validation? I have a form which uses a session variable to see whether a text field has been filled in (eg. if left empty then on submitting form, it is redisplayed with red asterisk against empty field. Those fields that are filled in, display the contents). This works fine except with drop down lists - any selection is not kept but displays first item in the list.

Can anyone help.
 
Old January 13th, 2004, 11:34 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You'll need to code this yourself. For a Text box, this is easy, as you probably found out:

' ASP Code
Dim TextBoxValue
TextBoxValue = Request.Form("MyTextBox")

' HTML Form
<input type="text" name="MyTextBox" value="<%=TextBoxValue%>">

For a drop-down, things are a bit more difficult. If you're using a program like Dreamweaver, you can have it automatically create the code for you. If you're doing it manually, you'll need something like this (taken from a Dreamweaver page). For each drop-down option element you want to display, you need to check whether the current Form value equals the value of the <option> element. If that's the case, you need to write out selected so the item appears selected in the browser:
Code:
<%@LANGUAGE="VBSCRIPT"%>
<html>
    <head>
    </head>
<body>
    <form name="frmTest" method="post">
    <select name="lstTest">
        <option value="1" <%If (Not isNull(Request.Form("lstTest")))  _
                Then If ("1" = CStr(Request.Form("lstTest")))  _
                Then Response.Write("SELECTED") :  _
                Response.Write("")%>>Item 1</option>
        <option value="2" <%If (Not isNull(Request.Form("lstTest"))) _
                Then If ("2" = CStr(Request.Form("lstTest"))) _
                Then Response.Write("SELECTED") : _
                Response.Write("")%>>Item 2</option>
        <option value="3" <%If (Not isNull(Request.Form("lstTest"))) _
                Then If ("3" = CStr(Request.Form("lstTest"))) _
                Then Response.Write("SELECTED") : _
                Response.Write("")%>>Item 3</option>
    </select>
    <input name="btnSubmit" type="submit" value="Submit to Server">
    <%
        If Request.Form("lstTest") & "" <> "" Then
            Response.Write("<br />Selected Item is " & _
                Request.Form("lstTest"))
        End If
    %>
    </form>
</body>
</html>
I split the code for the <option> elements over multiple lines using an underscore, but each <option> element could be on just one long line.

Just copy and paste the entire code block in a new ASP page. Run it in your browser, select an item and click the Submit button. You'll notice the item remains selected and the value of the selected item appears under the drop-down.

HtH,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old January 13th, 2004, 04:03 PM
Authorized User
 
Join Date: Jun 2003
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi,
if you put all of your optionlist in an array, you could do it this way with a loop: (you could also put it in a databasetable, then you'd have to open it upon a recordset and do a loop with a "while not recordset.eof .... recordset.movenext wend" instead of the loop here with the "for intcounter=0 to 3 do ... next"
-----------------------------
<%@LANGUAGE="VBSCRIPT"%>
<%
dim intCounter
dim optionList(2,1)
optionlist(0,0)=1
optionlist(0,1)="item 1"
optionlist(1,0)=2
optionlist(1,1)="item 2"
optionlist(2,0)=3
optionlist(2,1)="item 3"
%>
<html>
    <head>
    </head>
<body>
    <form name="frmTest" method="post">
    <select name="lstTest">
<%
for intCounter = 0 to 2
 response.write "<option value="""
 response.write optionlist(intCounter,0)
 response.write """"
 If request.Form("lstTest")=optionlist(intCounter,0)th en
  response.write(" SELECTED")
 end if
 response.write ">"
 response.write optionlist(intCounter,1)
 response.write "</option>"
next
%>
    </select>
    <input name="btnSubmit" type="submit" value="Submit to Server">
    <%
        If Request.Form("lstTest") & "" <> "" Then
            Response.Write("<br />Selected Item is " & _
                Request.Form("lstTest"))
        End If
    %>
    </form>
</body>
</html>
-------------
bye,
mndrx
 
Old January 14th, 2004, 11:52 AM
Authorized User
 
Join Date: Jan 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar

Many thanks. Worked like a dream





Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting from multiple drop-lists Earl Hickey SQL Language 4 July 9th, 2008 03:34 PM
Help with dynamic drop down lists MarkGT Classic ASP Basics 3 April 29th, 2008 06:05 PM
Linked Drop Down Lists contagiouss_blue Excel VBA 6 June 8th, 2005 09:02 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.