|
||||||
Using GET and POST in a VBScript CGI Web PageHow to Process CGI Variables in a VBScript Server Based ApplicationOne of the key tasks that any CGI web page must do is to process the GET and POST variables sent from forms. This can be difficult, but not for the VBScript programmer.
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 ProcessingThe programmer requires three files in order to process the CGI parameters:
The end result will be a VBScript CGI page that is able to accept inputs from the web site's users. An Example HTML FormA web site developer will normally add a form to a web page in order to illicit information from a user, something like: <form action=/cgi-bin/process_cgi.vbs method=post>
Enter your full name: <input name=full_name><br>
Enter your age:<input name=age><br>
Enter your address:<input name=address><br>
<input type=submit value="Send name">
</form>
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 PageThe VBScript CGI application called by the form will need to call the header.vbs file which will then process the CGI variables: #!c:\WINDOWS\system32\cscript.exe /nologo "C:\Program Files\Apache Software Foundation\Apache2.2\cgi-bin\header.vbs"
wscript.echo "Content-type: text/html" & vbcrlf
Set cgi = cgi_variables
wscript.echo "Hello " & cgi.item("first_name")
The most important step, therefore, is to process those CGI variables. Processing The CGI Inputs with VBScriptThe VBScript file (in this example it is called "header.vbs" for processing the CGI inputs will do two things:
Therefore the header.vbs file will start with: Set fso = CreateObject("Scripting.FileSystemObject")
Set original = FSO.OpenTextFile(WScript.Arguments(0))
firstline = original.ReadLine
ExecuteGlobal original.ReadAll
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: Function cgi_variables
Set cgi_hash = CreateObject("Scripting.Dictionary")
However, how the variables are processed will depend on the originating web page's sending method:
This information is accessed via the WScript.Shell object: Set cgi = CreateObject( "WScript.Shell" )
And the request method is stored within the shell's environment: request_method = cgi.ExpandEnvironmentStrings(%"REQUEST_METHOD"%)
This can now be used to extract a string containing all of the CGI data: Select Case request_method
Case "GET"
variable_string = cgi.ExpandEnvironmentStrings("%QUERY_STRING%")
Case "POST"
variable_string = WScript.Stdin.ReadAll
End Select
The CGI data will now be contained in the string "variable_string". However, if it is examined it will look something like: full_name=Fred+Smith&age=21&address=234+Main+St%2C+Somewhereville
and is found to contain some control characters:
These, therefore need to replaced: variable_string = replace (variable_string, "+", " ")
variable_string = replace (variable_string, "%2C", ",")
The String will now contain something like: full_name=Fred Smith&age=21&address=234 Main St, Somewhereville
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: variable_array = split (variable_string, "&")
This array can then be used to populate the scripting dictionary: for i = 0 to ubound(variable_array)
cgi_variable = split (variable_array(i), "=")
cgi_hash.add cgi_variable(0), cgi_variable(1)
next
Finally the dictionary is returned as the output from the function: Set cgi_variables = cgi_hash
End Function
With that everything is in place and ready for the user to start using the VBScript CGI application. Viewing the ResultsIf 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.
The copyright of the article Using GET and POST in a VBScript CGI Web Page in Windows Programming is owned by Mark Alexander Bain. Permission to republish Using GET and POST in a VBScript CGI Web Page in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||