Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP 3 Classic ASP Active Server Pages 3.0 > ASP Forms
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 13th, 2004, 11:18 AM
Authorized User
 
Join Date: Jan 2004
Location: , , .
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.
  #2 (permalink)  
Old January 13th, 2004, 11:34 AM
Imar's Avatar
Wrox Author
Points: 67,554, Level: 100
Points: 67,554, Level: 100 Points: 67,554, Level: 100 Points: 67,554, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,265
Thanks: 66
Thanked 1,428 Times in 1,408 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.
  #3 (permalink)  
Old January 13th, 2004, 04:03 PM
Authorized User
 
Join Date: Jun 2003
Location: , , Belgium.
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
  #4 (permalink)  
Old January 14th, 2004, 11:52 AM
Authorized User
 
Join Date: Jan 2004
Location: , , .
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar

Many thanks. Worked like a dream
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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



All times are GMT -4. The time now is 04:52 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.