Wednesday 30 November 2011

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:


Contact Us


Email
First Name:
Last Name:
Subject:
Comments:




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


Confirmation


<% Response.Write strFirstName %>,

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.




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.

http://www.aspwebpro.com/tutorials/asp/passvariableswform.asp

No comments:

Post a Comment