December 2007
Beginner to intermediate
310 pages
8h 8m
English
You want your CGI program to read values from Web forms for use in your program.
First, look at an example in Perl, which uses the popular CGI.pm module:
#!/usr/bin/perl
use CGI;
use strict;
use warnings;
my $form = CGI->new;
# Load the various form parameters
my $name = $form->param('name') || '-';
# Multi-value select lists will return a list
my @foods = $form->param('favorite_foods');
# Output useful stuff
print "Content-type: text/html\n\n";
print 'Name: ' . $name . "<br />\n";
print "Favorite foods: <ul>\n";
foreach my $food (@foods) {
print " <li>$food</li>\n";
}
print "</ul>\n";Next, look at a program in C, which does pretty much the same thing, and uses the cgic C library:
#include "cgic.h"
/* Boutell.com's cgic library */
int cgiMain() {
char name[100];
/* Send content type */
cgiHeaderContentType("text/html");
/* Load a particular variable */
cgiFormStringNoNewlines("name", name, 100);
fprintf(cgiOut, "Name: ");
cgiHtmlEscape(name);
fprintf(cgiOut, "\n");
return 0;
}For this example, you also will need a Makefile, which looks something like this:
CFLAGS=-g -Wall CC=gcc AR=ar LIBS=-L./ -lcgic libcgic.a: cgic.o cgic.h TABrm -f libcgic.a TAB$(AR) rc libcgic.a cgic.o example.cgi: example.o libcgic.a TABgcc example.o -o example.cgi $(LIBS)
The exact solution to this will vary from one programming language to another, and so examples are given here in two languages. Note that each of these examples uses an external library to ...
Read now
Unlock full access