Pass Variables with Form ASP Tutorial - ASP Web Pro
  Active Server Pages Programming by ASP Web Pro

ASP Tutorials

PASS VARIABLES WITH FORM

Ok, so you want to pass variables between your web pages. Good news, it's a piece of cake with ASP. As with just about everything else in ASP, there are many ways to do this. In this tutorial, we will show you how to pass variables from one page to another using a simple form.

Let's say you have a contact form on your website and you want to personalize the confirmation page for the user. The first thing you need to do is create a contact.asp page and paste the code below into your page:

<HTML>
<HEAD><TITLE>Contact Us</TITLE></HEAD>
<BODY>

<form name="ContactUs" method="post" action="contactconfirm.asp">
<table width="100%" border="0">
<tr><td><div align="right">Email</div></td>
<td><input type="text" name="Email" size="50"></td></tr>
<tr><td><div align="right">First Name:</div></td>
<td><input type="text" name="FirstName" size="50"></td></tr>
<tr><td><div align="right">Last Name:</div></td>
<td><input type="text" name="LastName" size="50"></td></tr>
<tr><td><div align="right">Subject: </div></td>
<td><select name="Subject">
<option>Select</option>
<option>Contact Snoopy</option>
<option>Request A Tutorial</option>
<option>Sales</option>
<option>Technical Support</option>
<option>Other</option>
</select></td></tr></table>
<table width="100%" border="0">
<tr><td>Comments:</td></tr>
<tr><td><textarea cols="75" name="Comments"></textarea></td></tr></table>
<p align="left"><input type="submit" name="Submit" value="Submit"></p>
</form>

</BODY>

Now, you've got your basic form. Simple enough, right? Next, create a contactconfirm.asp page and paste the code below:

<%
DIM strEmail, strFirstName, strLastName, strSubject
strEmail = Request.Form("Email")
strFirstName = Request.Form("FirstName")
strLastName = Request.Form("LastName")
strSubject = Request.Form("Subject")
%>

<HTML>
<HEAD><TITLE>Confirmation</TITLE></HEAD>
<BODY>

<% Response.Write strFirstName %>,

<p>Thank you for contacting us. We have received your message and will send a reply to your email address, <% Response.Write strEmail %>, as soon as possible.</p>

</BODY>

This is a nice way to personalize the users experience by displaying the first name and email address that they entered on the form page.

When passing variables with a form, the Request.Form initiates the request for the form data and the ("Email") specifies the form field we are requesting. In this case, we created our own variables: strEmail, strFirstName, etc and set their values equal to the data from their respective form field. Rather than creating your own variables and setting each of their values, you could simply place <% Request.Form("Email") %> directly in your confirmation page instead of the <% Response.Write strEmail %>. Again, this is a matter of personal preference. However, we recommend you use the variable technique, especially when you start dealing with more complex ASP pages.

Now, you can pass variables from page to page within your website. Piece of cake!

f you want to learn another way to pass variables between pages, be sure to check out our Passing Variables with QueryString Tutorial.

< Back to Tutorials


 

 

Main Menu
Home
ASP Scripts
ASP Tutorials
JavaScripts
Awards
Contact Us

Email This Page

Other Resources
ASP Web Hosting
Search Engine Submission
Programming Help

 

 

 

Other ASP Web Sites

Site Map

 

Last Updated:
04 February 2012