In the articles How to Create a Simple VBScript CGI Web Page and Professional CGI Web Development with VBScript, the VBScript programmer is given two techniques for creating and using CGI (Common Gateway Interface) web pages.
However, they both show just a one way process. They show how to send information from the server to the user, but not how to send information from the user to the server. That is, of course, done by using the GET and POST methods in a HTML form. Any VBScript CGI web page must, therefore, be able to handle the variables sent to the server.
The Files Required for VBScript CGI Parameter Processing
The programmer requires three files in order to process the CGI parameters:
- an HTML file containing a form. This will send the parameters to the VBScript CGI file
- the VBScript CGI file. This will call a VBScript "header" file
- the VBScript "header" file. This will process the CGI parameters and the return control to the VBScript CGI file
The end result will be a VBScript CGI page that is able to accept inputs from the web site's users.
An Example HTML Form
A web site developer will normally add a form to a web page in order to illicit information from a user, something like:
In this case all of the parameter are to be sent to a file named "process_cgi.vbs" which is in the server's cgi-bin
An Example VBScript CGI Web Page
The VBScript CGI application called by the form will need to call the header.vbs file which will then process the CGI variables:
The most important step, therefore, is to process those CGI variables.
Processing The CGI Inputs with VBScript
The VBScript file (in this example it is called "header.vbs" for processing the CGI inputs will do two things:
- provide the functionality that will turn the CGI inputs into variables that can be handled in VBScript code
- return control to the calling file (for a full discussion the reasons for this read Professional CGI Web Development with VBScript)
Therefore the header.vbs file will start with:
This code identifies the calling file and then runs the code contained within it.
The CGI variable processing will be placed into a function in the header file. This function will return a scripting dictionary (for more on those read How to Use the VBScript Scripting Dictionary). The scripting dictionary will contain all of the variables passed to the CGI web page from a form:
However, how the variables are processed will depend on the originating web page's sending method:
- the GET method sends the variables as a string as part of a URL
- the POST method uses the system standard input (or stdin). This invisible to the user
This information is accessed via the WScript.Shell object:
And the request method is stored within the shell's environment:
This can now be used to extract a string containing all of the CGI data:
The CGI data will now be contained in the string "variable_string". However, if it is examined it will look something like:
and is found to contain some control characters:
- & is the variable separator
- + represents a space
- %2C is instead of a comma(,)
These, therefore need to replaced:
The String will now contain something like:
And now this is in a format which (with a little more manipulation) can be loaded into the scripting dictionary. The ampersand (&) can be used as a separator to split the string into an array:
This array can then be used to populate the scripting dictionary:
Finally the dictionary is returned as the output from the function:
With that everything is in place and ready for the user to start using the VBScript CGI application.
Viewing the Results
If the form is saved into a file (for example "forms.html) on the web server then the user can view it and fill in the requested information (as shown in Figure 1 at the bottom of this article). As soon as the user clicks the submit button then their name (or at least the name that they entered will appear on the screen. And all of this is possible because of VBScript being able to process the CGI variables.