Documentation
Previous | Next | Home

Working with Data

Information can be exchanged between web pages and server through the use of web forms and embedded addressing, as well as within the server command header itself. Typically, users interact with a web-based system through the use of web forms. These are standard web pages with embedded controls in them such as text fields, list-boxes, and radio buttons. Once a user enters information in a form they can click on a submit or other such button to forward the information to the server. At the server the data can be parsed and acted upon depending on the design of the program processing the data.

A typical web form includes the following basic components: header, controls, and submit button, such as:


<form src="myapp.ap" method="post">
<input type="text" name="fname" value="George">
<input type="text" name="lname" value="Smith">
<input type="submit" value="Click here to submit the form">
</form>

The first line represents the header line and contains the source file to pass the data to on the server as well as the data passing method. The SRC entry can be any valid html-compatible file located on any server reachable via the web. It is not necessary to include the full URL, as in http://yourserver.com/myapp.ap if the file is located on the same server and within the same directory as the form itself.

WebSuite's primary method of programmatically manipulating data is through the use of Active Pages and the FGL programming environment. Active Pages typically contain the FGL commands and instructions necessary to process the data.

The METHOD entry identifies to the server how the data is being passed, and can either be GET or POST. Several other methods are available through HTTP but are restricted within WebSuite for security purposes.

The GET method will pass the data within the URL itself as name/value pairs, as in:

        myapp.ap?fname=George&lname=Smith

Values are separated from the page address by a single "?" character, while individual name pairs are separated by the "&" character. Special formatting is required within the overall string to prevent the server from misinterpreting the information. For example, the data string "Now is the time for all good men…" will be automatically formatted as: "Now+is+the+time+for+all+good+men…", and a file reference such as "c:\config.sys" will convert to "c%3a%5cconfig%2esys".

As a programmer, you typically do not have to worry about these embedded instructions as the conversions are automatically handled through data processing. However, the webEncodeUrl( ) and webDecodeUrl( ) functions are provided to programmatically provide conversion capability.

Two problems exist with using the GET method to transfer data. First, all of the data is visible to the user, which can be saved just like any other web address. Secondly, the web command line is limited to a maximum length of approximately 4096 bytes of data. Having said this, however, sometimes it is desirable to allow the user to save the address with the data intact as a complete URL.

The alternate to the GET method is the POST method, which hides the data within the HTTP server header and can transfer data in excess of 4096 bytes. Unfortunately, some older browsers implement the POST method differently and your forms may not work correctly in these instances.

In either case, the same process is used to extract the data for manipulation. WebSuite provides low-level functions to access the form data as well as high-level object access. In most cases, object use is the preferred method.

The following code is an example using the SESSION object within an Active Page to extract the data of the form created in the previous example:


<[
	mysession = new( "session", param( 1 ), param( 2 ) )

	fname = mysession.var( "fname" )
	lname = mysession.var( "lname" )
]>

The first line creates a SESSION OBJECT called "mysession" by using the NEW operator. The name of the object class is "session" and two parameters are past during its inception: param( 1 ) and param( 2 ). The first parameter represents the ID of the web server, while the second represents the ID for the current session. Both are passed by default to an Active Page and are accessed using the param( ) function.

Once the object is created, the VAR method within the SESSION OBJECT is used to access the individual form variables. This is regardless of whether the form used a GET or POST to transfer the data. The VAR method takes a single parameter, which identifies the name of the variable. This parameter is not case sensitive.

By default, all data passed within a form is automatically converted to string data. You can use the auto-casting capabilities found within the FGL programming environment to convert string data to other types, as in:

        age = 0 + session.var( "age" )

The same example utilizing low-level functions would look like this:


<[
	websrvr = param( 1 )
	sessionid = param( 2 )

	fname = webDocumentVariable( websrvr, sessionid, "fname" )
	lname = webDocumentVariable( websrvr, sessionid, "lname" )
]>

Instead of using an object, low-level functions are used to access the data. In this case, the webDocumentVariable( ) function is passed the server ID, session ID, and name of the variable to access. The same result is achieved as the previous example, though in an object-oriented world, the object method is generally preferable to low-level functions. FGL and Active Pages support both.

Typically, once you have the data, some element of validation would occur followed by calculation and presentation of the results. The following code extends the object example above to include validation and presentation:


<[
	mysession = new( "session", param( 1 ), param( 2 ) )

	fname = mysession.var( "fname" )
	lname = mysession.var( "lname" )

	// Validate the data
	errmsg = ""
	if ( strempty( fname ) )
		errmsg += "Must specify a FIRST name.<br>\r\n"
	end
	if ( strempty( lname ) )
		errmsg += "Must specify a LAST name.<br>\r\n"
	end

	// Check if errors found
	if ( ! strempty( errmsg ) )
		mysession.response( "ERRORS.<br><br>" + errmsg )
		return( 0 )
	end

	// Display results
	str =  "First name is " + fname + " and last is " + lname
	mysession.response(str )
	return( 1 )	
]>

The above example checks to see if the variables where correctly passed by the form. If not, the RESPONSE method of the SESSION OBJECT is called to return the error messages. Otherwise, the same method is used to return the results. Standard HTML code could have been appended to the previous example instead of the last three lines, as in:


<html>
<body>
The first name is <[ ! fname ]>
And the last name specified is <[ ! lname ]>
</body>
</html>

Regardless of the method, FGL and Active Pages provide a powerful set of resources for the processing and manipulation of web data. For additional information regarding Active Pages, see the next section. FGL documentation can be accessed directly from the West Coast Web Adventures web site.




(c) 2001 by West Coast Web Adventures, Inc., All Rights Reserved