Open Directory Site ASP Tutorials

ASP | XML | VBScript | JavaScript | ADO | CSS | XMLDOM | PHP | Operating Systems

Home >> ASP >> ASP Forms and User Input

ASP Forms and User Input

 

User Input

To get information from forms, you can use the Request Object.

A simple form example:

<form method="get" action="../pg.asp">

First Name: <input type="text" name="fname"><br>

Last Name: <input type="text" name="lname"><br>

<input type="submit" value="Send">

</form>

There are two ways to get form information: The Request.QueryString command and the Request.Form command.

Request.QueryString

The Request.QueryString command collects the values in a form as text.

Information sent from a form with the GET method is visible to everybody (in the address field). Remember that the GET method limits the amount of information to send.

If a user typed "Bill" and "Gates" in the form example above, the url sent to the server would look like this:

http://www.w3schools.com/pg.asp?fname=Bill&lname=Gates

The ASP file "pg.asp" contains the following script:

<body>

Welcome

<%

response.write(request.querystring("fname"))

response.write("&nbsp;")

response.write(request.querystring("lname"))

%>

</body>

The example above writes this into the body of a document:

Welcome Bill Gates

Request.Form

To collect the values in a form with the POST method, use the Request.Form command.

Information sent from a form with the POST method is invisible to others. The POST method has no limits, you can send a large amount of information.

If a user typed "Bill" and "Gates" in the form example above, the url sent to the server would look like this:

http://www.w3schools.com/pg.asp?fname=Bill&lname=Gates

The ASP file "pg.asp" contains the following script:

<body>

Welcome

<%

response.write(request.form("fname"))

response.write("&nbsp;")

response.write(request.form("lname"))

%>

</body>

The example above writes this into the body of a document:

Welcome Bill Gates

Form Validation

The form input should be validated on the browser, by client side scripts. Browser validation has faster response time, and reduces the server loads.

You should consider to use server validation if the input from a form is inserted into a database. A good way to validate the form on a server is to post the form into itself, instead of jumping to a different page. The user will then get the error messages on the same page as the form. This makes it easier to discover the error.

Example

1. Using Get Method

This example demonstrates how to interact with the user, with the Request.QueryString command.

Coding

<html>

<body>

<form action="demo_reqquery.asp" method="get">

Please type your first name:

<input type="text" name="fname"><br><br>

<input type="submit" value="Submit">

</form>

<%

If Request.QueryString("fname")<>"" Then

Response.Write ("Hello " & Request.QueryString("fname") & "!")

Response.Write ("<br>How are you today?")

End If

%>

</body>

</html>

2. Using Post Method

This example demonstrates how to interact with the user, with the Request.Form command.

Coding

<html>

<body>

<form action="demo_simpleform.asp" method="post">

Please type your first name:

<input type="text" name="fname">

<br>

<input type="submit" value="Submit">

</form>

<%

If Request.Form("fname")<>"" Then

Response.Write ("Hello " & Request.Form("fname") & "!")

Response.Write ("<br>How are you today?")

End If

%>

</body>

</html>

3. form with Radio Buttons

This example demonstrates how to interact with the user, through radio buttons, with the Request.Form command.

Coding

<html>

<body>

<%

dim cars

cars=Request.Form("cars")

%>

<form action="demo_radiob.asp" method="post">

<p>Please select your favorite car:</p>

<input type="radio" name="cars" value="Volvo"

<%if cars = "Volvo" then Response.Write("checked")%>>

Volvo V70

<br>

<input type="radio" name="cars" value="Saab"

<%if cars = "Saab" then Response.Write("checked")%>>

Saab 95

<br>

<input type="submit" value="Submit">

</form>

<%

if cars<>"" then

Response.Write("<p>Your favorite car is: " & cars & "</p>")

end if

%>

</body>

</html>

Cheap Web Hosting Articles - Web Site Design & Web Hosting Tutorials - Domain Hosting