Page 46 - LoudOffice_Guide-to-HTML_Part-II_Advanced.PDF
P. 46
VI. Adding CGI Scripts
The Common Gateway Interface (CGI) is the most commonly used method for passing
information from an HTML document to a server-side program that can process the
information. Since the program is run on the server, CGI doesn’t care what type of browser
is used. Normally, the method of input for a CGI script is a form, while the output is
normally some sort of HTML document. Of and about itself, CGI is not a programming
language but an interface between the actual program and the web server. CGI allows you
to write programs in many different languages, with Perl being the one of the most
common. In order to run CGI using Perl, you’ll need a website server that has a CGI bin
along with a Perl interpreter. Perl can be run on both Unix and Windows servers; however,
there are enough differences between the two that precludes us from going into great detail
in this introduction. For a full introduction, we recommend “Teach Yourself CGI in 24 Hours”
and “Teach Yourself Perl in 24 Hours” from SAMS publishing.
Adding CGI to Your Web Site
CGI programs are stored with extensions of . c g i or . p l . The simplest way to include CGI
on your website is to upload the files to your server via FTP and either run the program
directly or include the CGI on your form. CGI files are normally small in size, and free. You
can find free cgi scripts by simply going to your favorite search engine and typing in terms
like ‘free cgi scripts’ or ‘cgi resources’.
Below is an example of a CGI script to calculate a mortgage payment.
# ! / u s r / l o c a l / b i n / p e r l
#
$ a r g s = $ E N V { ' Q U E R Y _ S T R I N G ' } ;
p r i n t " C o n t e n t - t y p e : t e x t / h t m l " ;
p r i n t " < h t m l > \ n " ;
i f ( l e n g t h ( $ a r g s ) < 3 ) {
#
# T h e F o r m H a s n ' t B e e n S u b m i t t e d . D i s p l a y I n p u t F o r m .
#
p r i n t < < E O F ;
< h e a d > < t i t l e > S i m p l e L o a n P a y m e n t C a l c u l a t o r < / t i t l e > < / h e a d >
< b o d y b g c o l o r = f f f f f f >
< c e n t e r >
< h 1 > S i m p l e L o a n P a y m e n t C a l c u l a t o r < / h 1 >
< h r >
< p > E n t e r t h e v a l u e s a n d I w i l l t e l l y o u t h e p a y m e n t . T h e l o a n c a n b e
a m o r t g a g e , c a r l o a n , o r a n y o t h e r s i m p l e i n t e r e s t a m o r t i z a t i o n o v e r
a f i x e d t i m e w i t h f i x e d m o n t h l y p a y m e n t s . < / p >
< h r >
< f o r m a c t i o n = " s i m p l e . c g i " >
< t a b l e b o r d e r = " 1 " c e l l p a d d i n g = " 2 " c e l l s p a c i n g = " 0 " >
< t r >
< t d > P r i n c i p a l < / t d >
< t d > < i n p u t n a m e = " a m t " t y p e = " t e x t " v a l u e = " 1 0 0 , 0 0 0 " s i z e = " 1 0 " > < / t d >
< / t r >
< t r >
< t d > I n t e r e s t < / t d >
< t d > < i n p u t n a m e = " i n t " t y p e = " t e x t " v a l u e = " 8 . 0 0 " s i z e = " 1 0 " > < / t d >
< / t r >
< t r >
LoudOffice.com Guide to HTML – Part II Page 46