_ TWF  _ Code Chap 8

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
HOME | BACK | NEXT | MENU | SEARCH | ORDER | COMMENT | HELPINFO -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

08code01.pl


#!/bin/sh

echo Content-Type: text/plain

echo 

echo CGI/1.0 test-cgi script results:

echo 

echo argc = $#  argv = "$*"

echo

echo SERVER_SOFTWARE   = $SERVER_SOFTWARE

echo SERVER_NAME       = $SERVER_NAME

echo GATEWAY_INTERFACE = $GATEWAY_INTERFACE

echo SERVER_PROTOCOL   = $SERVER_PROTOCOL

echo SERVER_PORT       = $SERVER_PORT

echo REQUEST_METHOD    = $REQUEST_METHOD

echo HTTP_ACCEPT       = "$HTTP_ACCEPT"

echo PATH_INFO         = $PATH_INFO

echo PATH_TRANSLATED   = $PATH_TRANSLATED

echo SCRIPT_NAME       = $SCRIPT_NAME

echo QUERY_STRING      = $QUERY_STRING

echo REMOTE_HOST       = $REMOTE_HOST

echo REMOTE_ADDR       = $REMOTE_ADDR

echo REMOTE_USER       = $REMOTE_USER

echo CONTENT_TYPE      = $CONTENT_TYPE

echo CONTENT_LENGTH    = $CONTENT_LENGTH


08code02.pl


# determine the request method, make sure data is coming

# across STDIN, and determine if query string is defined

if ($#ARGV != -1  &&  $ENV{REQUEST_METHOD} eq 'GET' && 

                   $ENV{QUERY_STRING} ne '') {

    # split the query string into an array of keywords

    foreach $widget (split("&", $ENV{QUERY_STRING})) {

      # get the keyword and value pair from the widget string

      if ($widget =~ /(.*)=(.*)/)  {

         ($key, $value) = ($1, $2); 

          $value =~ s/\+/ /g ; # replace "+" with " "

          # unescape ASCII hexadecimal characters

          $value =~ s/%(..)/pack('c',hex($1))/eg;

          $inputs{$key} = $value; # add keyword/value pair to a list

      }

    }


08code03.htm


<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

<HTML>

<HEAD>

<TITLE>Altenative Software Solutions, Inc. Travel Form</TITLE>

</HEAD>

<BODY>

<H1>Alternative Software Solutions, Inc. Travel Form</H1>

<P> Please try to give Biff  (biff@ass.com) this information at least two weeks in advance.

<HR>

<FORM METHOD="POST" ACTION="http://www.ass.com/cgi-bin/travel_request.pl">

<INPUT TYPE="reset" VALUE="Clear Form">

<HR>

<H2>Employee Information</H2>

<HR>

<INPUT SIZE=40 NAME="fname"> First Name<BR>

<INPUT SIZE=40 NAME="lname"> Last Name<BR>

<INPUT SIZE=40 NAME="email"> Email<BR>

<INPUT SIZE=25 NAME="billto"> Department Billing Code<BR>

<HR>

<H2>Dates and Times</H2>

<INPUT NAME="ddate"> Departure date

<P>Preferred time of departure:<BR>

<INPUT TYPE="radio" NAME="dtime" VALUE="5AM-9AM"> <i>5AM-9AM</I>

<INPUT TYPE="radio" NAME="dtime" VALUE="9AM-12PM"> <i>9AM-12PM</I>

<INPUT TYPE="radio" NAME="dtime" VALUE="12PM-4PM"> <i>12PM-4PM</I>

<INPUT TYPE="radio" NAME="dtime" VALUE="4PM-8PM"> <i>4PM-8PM</I>

<INPUT TYPE="radio" NAME="dtime" VALUE="8PM-12AM"> <i>8PM-12AM</I>

<P><INPUT NAME="rdate"> Return date 

<P>Preferred time of return:<BR>

<INPUT TYPE="radio" NAME="rtime" VALUE="5AM-9AM"> <i>5AM-9AM</I>

<INPUT TYPE="radio" NAME="rtime" VALUE="9AM-12PM"> <i>9AM-12PM</I>

<INPUT TYPE="radio" NAME="rtime" VALUE="12PM-4PM"> <i>12PM-4PM</I>

<INPUT TYPE="radio" NAME="rtime" VALUE="4PM-8PM"> <i>4PM-8PM</I>

<INPUT TYPE="radio" NAME="rtime" VALUE="8PM-12AM"> <i>8PM-12AM</I>

<HR>

<H2>Airlines</H2>

<INPUT TYPE="checkbox" VALUE="indifferent" NAME="airline-ind"> Don't care which airline<BR>

<INPUT TYPE="checkbox" VALUE="profile" NAME="airline-prof"> Use my profile<BR>

<INPUT SIZE=40 NAME="airline1"> First choice <BR>

<INPUT SIZE=40 NAME="airline2"> Second choice <P>

<H3>Airline Seating Preference</H3>

<DL>

<DD><INPUT TYPE="radio" VALUE="indifferent" NAME="seating"> Don't care about seating

<DD><INPUT TYPE="radio" VALUE="profile" NAME="seating"> Use my profile

<DD><INPUT TYPE="radio" VALUE="window" NAME="seating"> Window

<DD><INPUT TYPE="radio" VALUE="aisle" NAME="seating"> Aisle

<DD><INPUT TYPE="radio" VALUE="emergency" NAME="seating"> Emergency row

</DL>

<HR>

<H2>Destination</H2>

<INPUT SIZE=45 NAME="city"> City<BR>

   <SELECT NAME="state" SIZE="3"> 

  	   <OPTION> Alabama

           <OPTION> Alaska

           <OPTION> Arizona

           <OPTION> Arkansas

           <OPTION> California

           <OPTION> Wyoming

</SELECT>State<BR>

<HR>

<P> Smoking preference:<BR>

<INPUT TYPE="radio" NAME="smoking" VALUE="no"> <i>Non-smoking</I> <INPUT TYPE="radio" NAME="smoking" VALUE="yes"> <i>Smoking</I>

<HR>

<H2>Reasons for trip</H2>

<TEXTAREA NAME="reasons" ROWS="5" COLS="55"></TEXTAREA><BR>

<HR>

<P>

<INPUT TYPE="submit" VALUE="Submit Travel Plans to Biff">

</FORM>

<HR>

<ADDRESS> Last updated Mon 11 Jul 94 by elvis@ass.com</ADDRESS>

</BODY>

</HTML>


08code04.pl


# flush stdout buffer

$| = 1;

# Print this MIME data no matter what

print "Content-type: text/html\n\n";

# check for the POST method

if ($ENV{'REQUEST_METHOD'} eq 'POST')

{

    # How many bytes are we supposed to receive?

    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

    # make a list of keyword/value pairs

    @pairs = split(/&/, $buffer);

    # cycle through each pair and decipher the values

    foreach $pair (@pairs)

    {

        # get the name/value pair strings

        ($name, $value) = split(/=/, $pair);

        # translate "+" to a space

        $value =~ tr/+/ /;

        # decipher ASCI hexidecimal escaped characters, if any

        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

        # add the pair to a list keyed on the name of the variable

        $contents{$name} = $value;

    }

}


08code05.pl


#!/usr/tools/bin/perl -- -*-perl-*-

# Print this out no matter what

print "Content-type: text/html\n\n";

# flush stdout buffer

$| = 1;

if ($ENV{'REQUEST_METHOD'} eq 'POST')

{

    # How many bytes are we supposed to receive?

    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

    @pairs = split(/&/, $buffer);

    foreach $pair (@pairs)

    {

		($name, $value) = split(/=/, $pair);

		$value =~ tr/+/ /;

		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

		$contents{$name} = $value;

    }

}

chop($date = `date`);

###############################################

# CGI Output: As HTML and as an email message

###############################################

# Create an HTML document on the fly. Yee-haw!

print <<"HTML";

<HTML>

<HEAD><TITLE>A.S.S. Travel Form Entries</TITLE></HEAD>

<BODY>

<H1>Travel Request from $contents{'name'}</H1>

<P><HR><P>

<I>$date</I>

<UL>

<LI> Name = $contents{'name'}

<LI> Email = $contents{'email'}

<LI> Departure date = $contents{'ddate'}

<LI> Departure time range = $contents{'dtime'}

<LI> Return date = $contents{'rdate'}

<LI> Return time range = $contents{'rtime'}

HTML



# check the airline section of the form data

if($contents{'airline-ind'} eq 'indiffernt') { ; }

else {

    print "<LI> Airline (first choice) = $contents{'airline1'}\n";

    if($contents{'airline2'} ne '') {

  	print "<LI> Airline (second choice) = $contents{'airline2'}\n";

    }

}

print <<"HTML";

<LI> Destination City = $contents{'city'}

<LI> Destination State = $contents{'state'}

<LI> Smoking preference = $contents{'smoking'}

<LI> Reason for trip = $contents{'reasons'}

HTML



if($contents{'reasons'} ne '') {

    print "<LI> Reasons = $contents{'reasons'}\n";

}

print <<"HTML";

</UL>

</BODY></HTML>

HTML

# Now create an email message and mail it.

$subject = "Travel Plans for " . $contents{'name'};

$sendto = "biff@ass.com";

# open a named UNIX pipe to send the mail

open (MAIL, "| /usr/lib/sendmail $sendto") || die "Can't send mail: $!\n";

# This selects the open pipe handle. 

select(MAIL);

print <<"EMAIL";

Date: $date

From: $contents{'email'}

To: $sendto

Subject: $subject



Name = $contents{'name'}

Email = $contents{'email'}

Bill-to = $contents{'billto'}

Departure date = $contents{'ddate'}

Departure time range = $contents{'dtime'}

Return date = $contents{'rdate'}

Return time range = $contents{'rtime'}

EMAIL



if($contents{'airline-ind'} eq 'indiffernt') { ; }

else {

    print "Airline (first choice) = $contents{'airline1'}\n";

    if($contents{'airline2'} ne '') {

  	    print "Airline (second choice) = $contents{'airline2'}\n";

    }

}

print <<"EMAIL";

Destination City = $contents{'city'}

Destination State = $contents{'state'}

Smoking preference = $contents{'smoking'}

Reason for trip = $contents{'reasons'}

EMAIL



if($contents{'reasons'} ne '') {

    print "Reasons = $contents{'special'}\n";

}

close(MAIL);

exit;


-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
HOME | BACK | NEXT | MENU | SEARCH | ORDER | COMMENT | HELPINFO -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

E-Mail: The Web Foundations at twf@lanw.com
URL: http://www.lanw.com/twf/codech08.htm
Text - Copyright © 1995, Ed Tittel, Mark Gaither, Sebastian Hassinger, & Mike Erwin.
Web Layout - Copyright © 1995, LANWrights & IMPACT Online.
Revised -- February 15th, 1996 [James Michael Stewart - WebMaster - IMPACT Online]

HTML 2.0 Checked!