Forms
In this lesson we will first study forms then we will proceed to processing form data in our asp programs.
To be able to accept information from user and send it to server side script program we must use forms in our html pages.
Below code is a simple html page with a form on it. Our simple form consists of two objects, a text box and a submit button.
Example 3-1: 3-1.html
<html>
<head>
<title>Simple Form</title>
</head>
<body>
<form method="get" action="subscribe.asp">
Enter your email here: <input type="text" name="email">
<input type="submit" value="Submit">
</form>
</body>
</html>
Contents of form come between <Form …> and </Form> tags. These are normally form fields such as text boxes, radio buttons, combo boxes etc.
Our form tag also uses two options or parameters. "Action” parameter specifies a script or cgi program on a server on the other side of http connection. This is the program responsible for processing the input.
Get method
Data can be sent to server side program using one of two methods ‘get’ and ‘post’. There are other methods which are used for other purposes.
‘get’ method sends input information by appending it to http url string. We will call this URL string as "Query String”. When you press submit key you will see that browser is trying to open a url like below one:
http://www.test.com/subscribe.asp?email=me@mydomain.com
As you see, browser has appended information to Query String and sends it in this way.
‘Get’ method has some limits. A browser may be unable to send url strings longer than specific length.
"post" method
A better method for sending information to server is ‘post’ method.
In this method information is sent in a data stream form over internet connection between browser and server.
In this method we do not have a limit on the amount of data that is being sent. Regardless of the method we use we will be able to retrieve values assigned to each input variable.
You can look at each input field as a variable. A value is assigned to each variable at client side. At server side we are able to retrieve "variable and value” pairs for each input field submitted.
In next section we are going to retrieve these information and use it in our ASP program.
Retrieving "get" method information
Retrieving form information in ASP is very simple. If you want to retrieve value of "email” field of our form and assign its value to a variable "eml”, you must write a simple code.
Eml=request.QueryString("email”)
After this you can use "eml” variable easily as it contains the value of "email” input field.
Save html form of example 3-1 and this asp script in a directory on your server and try to run it by browsing html page and submitting an email address.
Example 3-2: subscribe.asp
<%
eml=Request.QueryString("email")
%>
<html>
<head><title>Thank You!</title></head>
<body>
<br>
<center>
Thank you!<br>
Your email <b><%=eml%></b> added to our subscription list.
</center>
</html>
In above example we have used "QueryString” property of "request” object. As you saw later, we use "response” object to send information to web browser. We will use "Request” object to retrieve information sent by browser.
In next section we will use another property of "request” object to retrieve information sent to server program by post method.
Retrieving "post” method
Before trying to use post method let’s change our html file for next example. We must change method type to "post” this time.
Example 3-3: 3-3.html
<html>
<head>
<title>Simple Form</title>
</head>
<body>
<form method="post" action="subscr.asp">
Enter yor email here: <input type="text" name="email">
<input type="submit" value="Submit">
</form>
</body>
</html>
Retrieving information sent by browser using "post” method is very similar to "get” method.
eml=request.Form("email”);
As you see we just used "Form” property of "request” object instead of "QueryString” property.
Example 3-4: subscr.asp
<%
eml=Request.Form("email")
%>
<html>
<head><title>Thank You!</title></head>
<body>
<br>
<center>
Thank you!<br>
Your email <b><%=eml%></b> added to our subscription list.
</center>
</html>
We strongly suggest you to use post method whenever possible.
In this lesson you learned how you can create forms to send information to server side acript or cgi programs.
If you have worked on CGI programming, you have surprised to see how easy you can retrieve form data in your program. In CGI programs you may need several lines of program to do the same task.
In our next lesson we will continue working on forms. We will work with different types of input fields. We will also see more complicated examples.
Exercises
1- Design a html page which contains two fields : Name and LastName. Now write a program that receives user
name and lastname and prints a greeting message on browser screen.
2- Write a html page and an ASP program to accept birth date of a user (in XXXX) format. ASP program must calculate his age and print it on the screen.
Next Lesson
|