« Assignment 1 | Main | class 03 »

February 07, 2005

Assignment 2

Since I missed class, this is more my style of who I would do it, using the example code.

test.cgi

Code is in read more

This is the cgi:

#!/usr/bin/perl

#########################################################
# class02-file1.pl - Basic File I/O
#########################################################
# Use CGI Perl module to output any error msgs to browser
use CGI::Carp qw(fatalsToBrowser);

# Define the name of the script just for reference
$scriptName = "test.cgi";
$pageTitle = "thoughts";

# Obtain any variables submitted either in the URL string
# or by an HTML form via the GET or POST method, and
# place in the %INPUT_VARS hash array
%INPUT_VARS = {};
&input_vars_receive;
&input_vars_parse;

#########################################################
# Define which file we will be using on the server
#########################################################
$myFile = "/home/dimitri/public_html/blog/dweb/file1.txt";

#########################################################
# Define our widgets here and collect values from INPUT_VARS array
#########################################################
$textWidgetLabel = "Penny for a thought:";
$textWidgetName = "Thought";
$textWidgetValue = $INPUT_VARS{$textWidgetName};

$submitWidgetName = "dataAction";
$submitWidgetValue = "Enter";

$commentWasSubmitted = 0;

#########################################################
# Check our inputs and perform any file content updates
#########################################################
if ($INPUT_VARS{$submitWidgetName} eq "$submitWidgetValue")
{ # Form was submitted so check if the comment field actually had text
if ($textWidgetValue ne "")
{ # This is how you write text to a file in Perl
open INPTR, ">>$myFile";
print INPTR "

";
print INPTR "$textWidgetValue\n";
print INPTR "

";
close INPTR;

# Set a flag so that we know that we actually had a valid comment
$commentWasSubmitted = 1;
}
}

# Tell the browser what type of data to expect
print "Content-type: text/html\n\n";

#########################################################
# Print our initial HTML
#########################################################
print qq^


$pageTitle

^;


print qq^









$pageTitle


Using the code from the class notes I built a entery page which sends the text to a file. There is then a SHTML page which uses "SSI" to read the text in. I understand the basic concept, but because I missed class there are somethings I am not quite sure about.


$textWidgetLabel




^;

# Give the user feedback if they submitted something
if ($commentWasSubmitted==1)
{ print qq^









Its good to think!

read the thoughts

^;
}

print qq^

^;

exit;

#########################################################
# Takes data from an HTML Form or URL string
#########################################################
sub input_vars_receive
{ $formData = $ENV{'QUERY_STRING'};

if ($ENV{'REQUEST_METHOD'} eq 'POST')
{ read(STDIN, $formData, $ENV{'CONTENT_LENGTH'});
}
}

#########################################################
# Parses form info
#########################################################
sub input_vars_parse
{ local($name,$value,$pair);
local(@pairs) = split(/&/, $formData);

# Get parameter names, their values and copy into $INPUT_VARS array
foreach $pair (@pairs)
{ ($name,$value)=split(/=/,$pair);
$value=~tr/+/ /s;
$value=~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$INPUT_VARS{$name}=$value;
}
}


______________________________________________________________________

This is the shtml:

"http://www.w3.org/TR/html4/loose.dtd">



Thoughts

Take a thought and if you have one leave it

Posted by dimitri at February 7, 2005 10:03 AM

Comments

Post a comment




Remember Me?