日期:2008-10-26  浏览次数:20921 次

源程序下载在本站的源程序栏的HTML一代类型里,名为隐藏窗体的应用例子

Using hidden forms to transfer data from client side to Server side and vice-versa


Author: Sudhakar M
Level of Difficulty: Beginner
Languages covered: ASP, javascript
Pre-required reading: None
Date: Apr 19, 2000


Introduction

This paper discusses how to use hidden forms to transfer data from client side to server-side and vice-
versa. This approach is needed when you have an ASP application, with the individual pages dealing with
only a part of the whole data involved, but at a later time, such as exiting the application, all of the
data entered intermediately so far is needed.

One obvious approach is to use Session variables. Session variables need cookies to be enabled to work.
But what if cookies are disabled? Also in today's e-commerce applications there are scenarios where random
servers service the browser requests depending on the load (load-balanced), so there is no session
involved. This is an alternative approach to session variables and cookies.

The Approach

I will discuss the approach by a simple example. Suppose I have an ASP application with three pages
Name.ASP, Address.ASP, database.ASP. The user navigates from one page to another page entering the
information along the way in the first two pages. Also after the information is entered in each particular
page, the server side needs the information, to make some decisions, like to pull some more data out of a
database based on the entered values and send it to the next page. This process continues until the user
reaches the last page, when all of the previous data is committed to the database. The trickiest part is
the information entered in any of the intermediate pages and information deduced from the server side
after submitting a page will be needed anywhere in the subsequent pages and moreover all of the
information will be needed in the last page.

The following, Hidden.inc, include file declares the server side variables for all the state involved, a
helper function that will be used every time at the beginning of a page on the server side:


Var FirstName="";
Var LastName="";
Var Address="";

// The following SaveState() function will retrieve
// the submitted state from the hidden frame on any given page

Function SaveState()
{
FirstName = "";
FirstName = Request.Form("FirstName").Item;

LastName = "";
LastName = Request.Form("LastName").Item;

Address = "";
Address = Request.Form("Address").Item;

}


This include file will be included in every page on the server side.

The following is a typical client side include file, Formstate.inc, containing the hidden form, which will
have all the fields involved in the application:


<FORM NAME="FormState">
  <INPUT TYPE=HIDDEN NAME=FirstName>
  <INPUT TYPE=HIDDEN NAME=LastName>
  <INPUT TYPE=HIDDEN NAME=Address>
</FORM>


Now lets move on to the actual ASP pages where the interesting action happens. This is how Name.ASP might
look:


<%@ Language=javascript %>

<HTML><head><title>Name </title&