Passing 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:

<form name="ContactUs" method="post" action="contactconfirm.asp">
Email:  <input type="text" name="Email" size="50"><br>
First Name:  <input type="text" name="FirstName" size="50"><br>
Last Name:  <input type="text" name="LastName" size="50"><br>
Subject:  <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>
Comments:  <textarea cols="75" name="Comments"></textarea><br>
<input type="submit" name="Submit" value="Submit">
</form>

 

Now, you’ve got a basic form.  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")
%>

<% 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>

 

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, 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 using a form.  Piece of cake!

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

< Back to ASP Tutorials