| Active Server Pages: Using Variables, and Forms |
With forms, there are two steps: first you create the form, and then you process it. To create a form for an Active Server Page, just create a standard HTML form.
To try out this example, create an HTML file ("form_response.html")
and cut-and-paste the following text into it.
| form_response.html |
| <html>
<head><title>Asking for information</title></head> <body> <form method="post" action="form_response.asp"> Your name: <input type="text" name="name" size="20"><BR> Your email: <input type="password" name="email" size="15"><BR> <input type="Submit" value="Submit"> </form> </body> </html> |
Active Server Pages provide a mechanism for processing forms that, unlike CGI scripting, doesn't involve serious programming: the Request.Form.
Considering the form above, we may create the file bellow and get a
response.
| form_response.asp |
| <html>
<head><title>Responding to a form</title></head> <body> Your name is <% =Request.Form("name") %> <BR> Your email is <% =Request.Form("email") %> </body> </html> |
To display the contents of each field in the form, type:
<% =Request.Form(fieldname) %>where fieldname is the name of the field.
For example, if you have a field called "CatName" in your form, you can save it into a variable called "TheName" by typing:
<% TheName = Request.Form("CatName") %>
If you want to display "VisitorName" several times within a text you only
need to include the variable in the text. For example:
My cat´s name is <% =TheName %>. Do you want to see <% =TheName %>?.
| nameandcolor.html |
| <html>
<head><title>Name and Color</title></head> <body> <FORM ACTION="nameandcolor.asp" METHOD=POST> Let me know your Name and Favorite Color: <P>YOUR NAME: <INPUT TYPE="TEXT" NAME="YOURNAME" SIZE=20> <P>COLOR: <INPUT TYPE="RADIO" NAME="COLOR" VALUE="1" CHECKED>Red <INPUT TYPE="RADIO" NAME="COLOR" VALUE="2">Green <INPUT TYPE="RADIO" NAME="COLOR" VALUE="3">Blue <P> <INPUT TYPE="SUBMIT" VALUE="OK"> </FORM> </body> </html> |
Now, create an ASP file ("nameandcolor.asp") and cut-and-paste the following
text into it.
| nameandcolor.asp |
| <html>
<head><title>Name and Color</title></head> <body> <% TheName = Request.Form("YOURNAME") %> <% colornumber = Request.Form("COLOR") %> Hi, <% =Thename %>.<BR> I know your favorite color is <% if colornumber = "1" then %> red <% end if %> <% if colornumber = "2" then %> green <% end if %> <% if colornumber = "3" then %> blue <% end if %>. </body> </html> |