Google
 

Wednesday, July 16, 2008

Active Server Pages: Using Variables, and Forms

Using Variables, and Forms in Active Server Pages

Forms are a convenient way to communicate with visitors to your Web site. Using forms, you can create a survey form and ask visitors to fill it out. When they fill out the form, you can process the results automatically.

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 try !

Asking for information


Your name: name" size="20">

Your email: email" size="15">




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 try !

Responding to a form

Your name is <% =Request.Form("name") %>

Your email is <% =Request.Form("email") %>

To display the contents of each field in the form, type:

<% =Request.Form(fieldname) %>
where fieldname is the name of the field.

Creating a Variable

You'll probably want to do more with your forms than display their contents in a Web page. For example, based on the contents of the form, you may want to create a variable and insert that variable in different places of your response page. You may need to create a variable. To do that, just make up a name and set it equal to the contents 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 %>?.

Example

The form in this example asks users to introduce their names and their favorite color: red, blue, or green. When the form is received, the server responds displaying these data.
nameandcolor.html try !

Name and Color


Let me know your Name and Favorite Color:

YOUR NAME:
YOURNAME" SIZE=20>

COLOR:
COLOR" VALUE="1" CHECKED>Red
COLOR" VALUE="2">Green
COLOR" VALUE="3">Blue





Now, create an ASP file ("nameandcolor.asp") and cut-and-paste the following text into it.

nameandcolor.asp try !

Name and Color

<% TheName = Request.Form("YOURNAME") %>
<% colornumber = Request.Form("COLOR") %>
Hi, <% =Thename %>.

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 %>.


No comments: