[Ilugc] [TIP] CGI samples

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Sun, 20 Nov 2011 08:37:01 +0530

All of you might have an idea about document root for a web server.

That is the base directory from which static html pages are dished out.

Likewise there is a cgi-bin alias directory. You have to run all your
CGI scripts
 from under that.

You will realise that CGI is not too much fun in itself. Only when
Ajax and javascript(Ajax
 means javascript) come together we will have fun.

Today we are only looking at form filling and data transfer between
user and backend using CGI.

The browser signifies the user who enters a value for search or a
select drop down. Based on this
 the server can fetch some data dynamically.

Okay let us get started.

As I said yesterday CGI is all html. So all the html constructs can be
used with CGI.

You can google for CGI form elements and you can also read the man page of CGI.

$ man CGI

will tell you the way you can use the perl CGI module to construct
simple CGI scripts like
this.

$ cat first.cgi
#!/usr/bin/perl

use CGI qw/:standard/;
print
header,
        start_html('Simple Script'),
        h1('Simple Script'),
        start_form,
        "What's your name? ",textfield('name'),p,
        "What's the combination?",
        checkbox_group(-name=>'words',
                        -values=>['eenie','meenie','minie','moe'],
                        -defaults=>['eenie','moe']),p,
        "What's your favorite color?",
        popup_menu(-name=>'color',
                        -values=>['red','green','blue','chartreuse']),p,
        submit,
        end_form,
        hr,"\n";

if (param) {
        print
                "Your name is ",em(param('name')),p,
                "The keywords are: ",em(join(", ",param('words'))),p,
                "Your favorite color is ",em(param('color')),".\n";
}
print end_html;

The same CGI script is used for both asking for data and showing the
results after
obtaining the data input by the user.

As in this case:

http://tulip.pye.org/cgi-bin/first.cgi

Ok this is bit of fun.

But you cannot do any animations with CGI,not even a simple page refresh.

Once you have browser cookies, javascript and JSON you can do quite a
bit of magic.

But the key to all this is Ajax(Asynchronous javascript and XML).

I dunno what the XML in it means as I have only used HTML which can be
argued as a subset
of XML.

Now CGI in its raw form has problems; like the page will be loaded in
full instead of a portion of
 a web page(a div). The only way to fight problems with CGI is to
combine Ajax and js magic.

Now Google Maps uses Ajax in a big way. And the only way you build
very user friendly apps
 in the browser is by using js techniques with CGI.

HTML5 promises 3D and drawing and several other things like gradients,
shadows and so on.

The canvas tag allows a lot of cool things and this works even in IE
with the explorercanvas js lib.

But first get yourself familiar with CGI.

Another useful CGI is;

#!/usr/bin/perl
use CGI;
$q = new CGI;

print $q->header;
$out = `fortune`;
print $out;

You can reload the page with F5 to get a new page everytime.

You can run just about any program inside the CGI script provided it
does not use
 nCurses or requires root permissions or relies on user input.

CGI scripts normally run as a different user from root. This is to
guard against web exploits.

CGI uses either of HTTP GET or HTTP PUT to obtain form filled data.

Now you see that CGI does its job using HTML and HTTP.

HTTP only has few methods.

GET
HEAD
PUT
OPTIONS
TRACE
POST

In the case of GET the parameters are passed to the CGI in the URL
itself. That is why you
 find Google search URL having the search term.

in the case of POST it goes as part of the page and in both cases the
CGI script sees the params
passed in standard input.

What CGI prints on standard output goes to the browser HTML rendering.

This gives a powerful dynamic HTML building capability.

Even javascript can write HTML and manipulate DOM.

We will see what a DOM is tomorrow.

-Girish


-- 
G3 Tech
Networking appliance company
web: http://g3tech.in ?mail: girish at g3tech.in

Other related posts: