Retrieving Form Data With ASP
By Stephen Bucaro
We are all familiar with those forms on the web where you type some information into
a text box, set a checkbox, or select an item in a list, and then click on the Submit
button to send the information off to the server. An HTML form allows a webpage to display
input fields and controls where users can enter data and submit it to the server. The
HTML code for an example form is shown below.
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
Message: <textarea name="message"></textarea><br>
<input type="submit" name="submit" value="Submit">
The code above would create a typical; contact form. After the user enters the appropriate
data in the form's fields and clicks on the Submit button, the browser will construct a
querystring containing the names and contents of the forms two text boxes and the textarea.
The querystring will be appended to the webpage's URL.
For example, if the user entered the name "Bob Sled", the email address "firstname.lastname@example.org" and
the mesage "good morning", the URL with the querystring appended would be as shown below.
This line would appear in the browsers address bar. Note that the querystring is separated
from the URL with a question mark, and the querystring consists of name=value pairs separated
by ampersands. Spaces are replaced with plus signs.
It's interesting to know that you can test your asp form processing code by typing the
URL?querystring directly into the browsers address bar, or by formatting the URL?querystring
as a link and clicking on it. The form is not required.
This example used the <form> tag with no attributes. In that case the form is submitted
to the same webpage that contains the form. You could submit the form to a different webpage
by adding the "action" attribute to the <form> tag, as shown below.
This example submitted the form with the default get method. Form data submitted by the
get method can be read by a server-side ASP script using the Request.QueryString
method as shown below.
Dim strName, strEmail, strMessage
strName = Request.QueryString("name")
strEmail = Request.QueryString("email")
strMessage = Request.QueryString(""message")
There are several things to consider when submitting a form with the get method. Some older
browsers and web servers limit how long a querystring can be. the querystring is visible in
the browser's address bar. If the form contains hidden data or the user enters a password,
these will be visible to anyone standing near the screen.