ASP Forms
(Page 1 of 5 )
In our last tutorial we learned some of the basics of ASP, like syntax, printing data to the screen, how to create and manipulate variables, and even how to make arrays. I even slipped a for loop in on you to further boggle your puny brain. In this article, I detail the use of forms in ASP and how to retrieve user input.
Forms
If you didn't cheat and answered the questions in my first tutorial, then you should have a grasp of how to create forms in HTML. Here, I am going to teach you to retrieve the data the user enters into those forms. The first method we will discuss is the request.querystring.
Request.QueryString
The Request.QueryString command is used to retrieve values in forms with the Get Method (method="get"). That's similar to the method my girlfriend uses every time I get near near her, except that method is the Git! Method and involves a lot of door slamming.
Data received from a form that utilizes the Get Method is visible to the public, as it is contained in the URL, and can only store limited information. Here is an example of how to use the Request.QueryString command:
<html>
<body>
<form action="sample.asp" method="get">
Please Enter Your Name: <input type="text" name="name"/>
<input type="submit" value="Submit" />
</form>
<%
dim name
name=Request.QueryString("name")
If name<>"" Then
Response.Write("Hello " & name & "!<br />")
Response.Write("You are my lord and savior!")
End If
%>
</body>
</html>
The above code asks the user for their name and stores the value when the user clicks the Submit button. It then takes that value and says if the value is not equal to (<>)""(or basically so long as the value is not null), then write "Hello" to the screen and append the text in the variable "name," followed by more text: "You are my lord and savior". If the user does not enter any text into the text field, nothing will happen.
Here is another example. First, let's create our HTML form:
<form method="Get" action="sample.asp"
Enter Your Name: <input type ="text" name ="name"/>
Enter Your Occupation: <input type ="text" name ="occupation"/>
<input type="Submit" value="Click to Submit"/>
</form>
Next, we will create our sample.asp page to process the information:
<%
Dim name, occupation
name="Request.QueryString("name")
occupation=Request.QueryString("occupation")
response.write("Your name is: " & name & "<br />")
response.write("Your current occupation is " & occupation & "<br />")
%>
If the user entered their name as Freddy Krueger and their Occupation as Crazy-assed Murderer, the following would be output to the screen:
Your name is: Freddy Krueger
Your current occupation is Crazy-assed Murderer
Request.Form
To collect data from forms that used the Post method (method=post), we use the Request.Form command. Unlike the Request.QueryString, the Request.Form command is invisible to the user (and thus will not appear in the URL), and there are no limits to the amount of data you can return.
To begin, let's create our HTML page with the form on it:
<form method="Post" action="anothersample.asp">
Enter Your Name: <input type="text" name="name"/>
Enter Your Occupation: <input type="text" name="occupation"/>
<input type="submit" value="Click to Submit"/>
</form>
Next we create our ASP page (anothersample.asp) to retrieve the data:
<%
Dim name, occupation
name=Request.Form("name")
occupation=Request.Form("occupation")
response.write("Your name is: " & name & "<br />")
response.write("Your present occupation is: " & occupation & "<br />")
%>
This will result in the following printing out to your browser (if the user had supplied the name Jason Voorhees and the occupation Camp Counselor):
Your name is: Jason Voorhees
Your present occupation is: Camp Counselor
Next: Cookies >>
More ASP Code Articles
More By James Payne