Retrieving text box values & Request.form()

If you are using the POST method for your form, you will need to use the Request.form() method to process the form. For simplicity, suppose this is our form:

request form

Note Information you submit with this form is not stored or validated! Please limit your input to 10 characters!

The following shows the HTML code that creates the form shown above:

<form method="post">
Telephone number: <input type="text" name="PhoneNumber" size="10" />
<input type="submit" value="Submit Phone" />
</form>

Our <form> tag starts the form. In the tag, we specify that we want to use to the POST method. Next, we create two input tags: the first tag creates the text box for the user to enter phone number and the second creates the submit button. Then, we end the form with the </form> tag.

We next discuss how to process the form. Recall that if you are using the POST method for your form, you will need to use the request.form () method. The following shows the specific code process the above form:

<%
response.write "Phone Number you submitted:" & request.form("PhoneNumber")
%>

The responde.write () prints “Phone Number you submitted:” along with the phone number. The request.form(“PhoneNumber”) simply collects what was submitted. Notice that here we are not checking whether or not the user has submitted the form. In other words, our one-line-of-code in its present form, will execute even if the user has not yet submitted the form. The discussion below explains how to resolve this issue with a different example.

<form method="post">
Favorite website URL:: <input type="text" name="FavoriteURL" size="15" />
<input type="submit" value="Submit URL" name="SubmitFavURL" />
</form>

In the code above, we add the name attribute to help us determine whether or not the form is submitted. If the form has not been submitted, the value of the attribute SubmitFavURL will not equal “Submit URL.” If, however, the form is submitted, the value of the attribute SubmitFavURL will equal “Submit URL.”

Now, let’s write a script that will determine whether or not the above shown form is submitted. We will need to construct a condition that will check the value of SubmitFavURL:

<%
if request.Form("SubmitFavURL") <> "" then
response.write "You submitted: " & request.Form("FavoriteURL")
end if
%>

The IF condition above checks if SubmitFavURL is empty or not. If it is not empty, then execute the response.write statement to print the user’s input. Otherwise, do nothing.

To see the output of our code, use the form below to submit your favorite website:

SubmitFavURL

Note The information you submit is not stored or validated! Please limit your input to 40 characters!