CGI Programming using PowerBASIC - Part 1: Basic Concepts

By Joe Byrne

Few of us question the 'power' of PowerBasic, but when it comes to web technologies, even the best developer can look a bit confused. And rightly so. After all, the web is relatively new in computer years and its acceptance as a real development environment is still being decided.

However, PowerBasic can be used, and quite successfully, for creating very sophisticated web based applicatons. These programs are typically called CGIs - an acronym standing for "Common Gateway Interface". Simply put, the web server acts as a butler between a standard web page (created with HTML code) and your PB executable program. The "gateway" portion is the web server that passes data to your program, and returns your output to the user in the form of an HTML document.

This is part one in a series that will explain the detailed workings of how PB CGIs are built, and how to implement them. I will start with some very simple concepts to get us pointed in the right direction, and lay the foundation for how the CGI app differs from what we traditionally create. It is also important to understand the role non-PB elements play in successfully writing web based applications so we will look at those items in a future article as well. For now however, lets look at a simple web application using PowerBasic to create a Windows based CGI application.

The basic premise of web based programming, is to use a standard HTML document to gather information from the user, pass that information to your CGI program, manipulate that data, then return something to the user. The web server sits in the middle of this process routing the necessary data back and forth. Therefore, a basic understanding of HTML is very helpful. Personally, I use a WYSIWYG HTML editor and leave the actual HTML code generation to the program.

Our first example, while not fancy, will demonstrate the basics of CGI programming. This is a standard "Contact Us" web form. It consists of 4 input fields: Name, Email Address, Phone number, and an area for a text message. When you click the ‘Submit’ button, the values you type in these fields will be passed on to our PB program called "contact.exe" and then returned to the user in the form of an HTML page. Not very sexy, nor practical, but it will demonstrate some every important aspects of CGI programming.

I will be using PB/CC for the CGI application mainly because the console compiler has the necessary I/O capabilities built in, but PB/Win is also capable of creating the necessary code. If you need to use PB/Win, you should use this PBCGIBOTH.inc file.



The HTML Web Page

Here's what the example web form looks like:

PowerBasic CGI Example #1
A Simple 'Contact Us" application

Your Name:

Email Address:

Phone#

Message:




There is no fancy HTML coding going on here. The actual HTML code to create the form displayed above is:

The main part to examine is the bolded 'FORM' tag. This creates the block of code that informs the server what to do when a ‘submit’ button is clicked. In this case, the data in each of the form fields will be sent to the program called contact.exe that is located on the web server in the document root folder in the cgi-bin directory. This is a standard location for cgi programs to reside, but is totally dependent upon how the web server is configured. The method used to send the data is POST. We will discuss the nuances of the transmission methods later on, but the POST method basically tells the server to pass the data to our program as one long string using the standard I/O port (STDIO). The form data is sent in name=value pairs so our CGI application needs to parse out these pairs based on each of the form elements names. You’ll see this in the next section:

The second part of the process is the actual PB CGI application itself. You can hardcode the logic to read the data stream from the STDIO port and parse the name=value pairs yourself, but, as I stated earlier, PowerBasic already supplies a great set of ‘wrapper’ functions in the PBCGI.INC file. In the following program, you can replace this with the pbcgiboth.inc file if you’re using PB/Win (make the syntax modifications as needed).



The PBCGI Program CONTACT.BAS

If you fill in the Web form and click the submit button, you should get a new web page that simply sends back the data you entered. It should be noted that this 'reply' web page does not exist as an HTML file anyplace. It is created 'on-the-fly' by our CGI program and the web server dutifully sends the HTML commands back to the user as a valid Web page.

While this is not very useful, please understand that the CGI application could have done anything you wanted with this data, such as send it to you via email, or stored it in a database, or looked up the name in a file to see if this person had sent a message previously. The potential use is limited only by your imagination.

Well, that’s it for part one. I hope this clears up a little bit about how you can leverage the Web and PB to create centralized applications. In future articles, I’ll discuss more advanced concepts including some wonderful HTML tricks you can use to make very sophisticated programs based on the same concepts we’ve seen here.