|
Unlike previous examples, this one does less hand-holding.
By now, you should feel comfortable getting a CGI script running on your web server.
TERMS
Remember that the browser sends a Request
and the web server sends a Response.
|
This is a useful example.
It shows the request from any HTML form,
plus a lot of stuff you might not have known visitors were sending.
This example is useful for debugging forms that are acting up.
- Start a new VB project.
Remove any forms from the project.
Add Text.Bas and VbWeb.Bas modules.
- Make a copy of the SampleMain procedure from the VbWeb.Bas module,
paste it into its own module,
and rename the procedure to Main.
- Make an executable out of the VB project
and put the executable in your web server's Standard CGI directory.
- Make an HTML page that contains a form
that is as simple or complicated as you like.
Set the ACTION property of the FORM tag
so that it points at the executable.
Here's a simple page example.
- Load the page into your browser,
enter some data in the form, and submit it.
|
About the Response
When you submit the form,
you should get back a page full of terms or Name/Value pairs.
Most of the terms do not come from your form.
Most of them are Server Variables
that come from a variety of places
such as the visitor's browser and your server.
Terms from your form should show up
at the bottom of the Response page.
|
About the Source Code
There are two important aspects to the SampleMain procedure.
One is the way it navigates through the entire Request collection.
The other part is all the code around it.
That code is every thing that should be in all your Main procedures.
Navigating the Request Collection
Here's the code that puts all the terms in the Response page.
For Each Name In Names
For Each Value In Request(Name)
Send Name, "=", Value, vbCrLf
Next
Next
You will have a better time understanding this code
once you get to the Request collection documentation.
For now, let me answer two common questions.
-
If you are familiar with the VB collection object,
then you might notice that Request is not the only collection.
There is also a Names collection.
The Names collection is needed since
you cannot retrieve the Key from a collection item.
The Name part of a term is used as the Key to the Request collection items.
-
You might notice that the code can send more than one Value for each Name.
This is because a form in an HTML page can contain
two (or more) controls with the same name.
When the visitor submits such a form,
the browser sends two terms with the same name in the request.
The Ultimate Main Procedure
The SampleMain procedure contains everything a Main procedure needs.
You might recognize the following part
of SampleMain from the Hello World example.
SendLn CGI_CONTENT_TYPE_TEXT_HTML
If you are sending back an HTML page
(which is almost always),
then this must be the first line you send.
The Hello World example sends the same page
regardless of how the page was requested.
So, there's no need to have a Request collection.
But in this example there is.
That's what LoadRequest is for.
Needless to say, LoadRequest must get called early in the program.
Unlike your typical VB application,
if an error occurs, the user does not get an error message.
Instead they just wait until they give up.
That's what makes an error trap so important.
The ErrorHandler procedure sends a simple page
so that the visitor knows something is wrong.
|

|
|

|
|
|