Forms and CGI scripts


Usually CGI scripts and forms live together. In fact usually scripts receive some data from users, handle those information and finally show results to users (usually by building a web page 'on fly'). However you could also run a CGI script simply by clicking on a link (that happens when the link point to a CGI script instead of an HTML document). So, in order to write CGI scripts, you have to know how forms work. A form is just a web page which contains some input fields (to type information into) and one or more buttons (to 'submit' the form and send data). Besides you have to specify the CGI script which will handle those information. But let's start by watching a simple form. The HTML source follows:

<html><head><title>This is a simple form</title></head>
<body>
<FORM METHOD="GET" ACTION="http://www.anyserver.com/cgi-bin/formsname">
<p>Type something here: <INPUT NAME="nameoffield"></p>
<p>INPUT TYPE="SUBMIT"></p>
</FORM>
</body></html>

<FORM></FORM>

By means of these tags you are saying to the web browser (your client): 'hey browser! This is a form and you have to read it in order to send data to the specified server'.

METHOD="GET"

By means of the 'METHOD' keyword, you are saying to the web browser HOW it has to send data. You can specify 2 way: GET or POST. If you specify GET as a method, data will be sent linded up at the end of the URL, otherwise, by using the POST method, data will be sent as a distinct HTML object to the web server (we will see later the real difference between these methods). Warning don't be confused with HTML and HTTP methods! Here I'm talking about HTML METHODS (how to send data). HTTP's GET method for example (contained inside of the 'chat' between a client and a server), is a completely different thing (in fact it means that the web server has to retrieve a specified document).

ACTION="http://www.anyserver.com/cgi-bin/scriptname"

By means of the 'ACTION' keyword, you are saying to the web server WHAT it has to do, or rather, WHAT SCRIPT it has to run and WHERE it has to find it. So anyserver will execute the scriptname script, which is located inside of the cgi-bin folder. Cgi-bin is not just a simple folder: it's a particular directory. In fact you can configure a web server in order to let it know where it can find CGI scripts. Usually CGI scripts are contained inside of the cgi-bin directory. However web server can be configured to run files with 'cgi' extension (in this case the 'script.cgi' file for example, can be executed as a CGI script by the web server).

INPUT NAME="nameoffield"

This is an input field, and here users can type the requested information. The CGI script will look into a variable (in this example the name of that variable is 'nameoffield').

INPUT TYPE="SUBMIT"

Well, this is just a button to submit the form. After filling out the form, users can send data by pressing this button. If you specify GET as a method, data containsed inside of INPUT fields are lined up at the end of the URL, after the '?' symbol. Besides every fields and their respective names are separated by the '=' symbol. In the previous example, when you press the submit button, the web browser forms this URL:

http://www.anyserver.com/cgi-bin/scriptname?nameoffield=something

If you have more input field, the browser puts the '&' symbol between each field. For example, suppose you have 2 input field named 'field1' and 'field2', well the URL will be:

http://www.anyserver.com/cgi-bin/scriptname?field1=something&field2=something

Where 'something' is any thing the user typed into the field. If the user types 2 or more words inside of an input field, the browser puts the '+' symbol between each of them. In other words it substitute spaces with the '+' symbol. So suppose you have 2 field: 'yourname' and 'yourage'. Then suppose the user fill out the form so:

yourname = Robert Clark

yourage = 99

Well, the URL will be:

http://www.anyserver.com/cgi-bin/scriptname?yourname=Robert+Clark&yourage=99

All special characters such as =, &, #, £, %, / and so on, are subsituted by '%'. So, in the previous example, the user could write:

yourname = Rober Clark & Company

And the URL after the script name would become:

yourname=Robert+Clark+%&+Company&yourage=99

Warning: when the user press the submit button, the web browser build the URL, and variables are sent lined up at the end of the URL istself. When the web server receive data, variables are stored inside of a particular environment variable called PATH_INFO. The CGI script can read that variable, but it has to decode, because it contains all indesiderable symbols like &, +, = and so on. We let's see more about environment variables later.


Index           Home  Back      About  Contact us!

Copyright (c) 1998-2006 Wowarea