Gossamer Forum
Home : General : Perl Programming :

Patchy output in my file?

Quote Reply
Patchy output in my file?
Patchy output from script???

Hi,
I had this script working OK but I decided to try and add another script to take over error checking and then pass the values from the form to my main script but now I get patchy output in the output pages.

Why is this?



Error Checking Script:
Code:


#!perl

#Declare the path to the perl interpreter

#include the library for parsing input
use CGI;

#parse the form arguments
if ( !(CGI::ReadParse(\%values)) )
{

}








#variables to hold data if no data entered output message
if ( !($name=CGI::param(
"name")) )
{
#output error page if no data entered
&outputErr(
"Please enter your name - Hit the
back button and enter the missing information
");
}

if ( !($address=CGI::param(
"address")) )
{
#output error page if no data entered
&outputErr(
"Please enter your address - Hit the
back button and enter the missing information
");
}

if ( !(@items=CGI::param(
"items")) )
{
#output error page if no data entered
&outputErr(
"Please select an item - Hit the
back button and enter the missing information
");
}

#output web page
print
<<PAGE ;
Content-type: text/html \n\n
<html>
<head>
<title>Order</title>
</head>
<body><body bgcolor="#000000" >
<h1><center><font color="white"><i><b>$name</b> </i></h1>
<p>
<i>Of</i> <b><font size="+1"><i>$address</i></b></font>
<p>
<i>You ordered item{s}</i> <b><i><font size="+1">@items</i></b></font>
<p>
<i>Please check your order for errors if <br>
the information is correct please submit:</i></font>
<br>
<br>
<form action=icak1.cgi method=post>

<P>

<INPUT TYPE="HIDDEN" NAME="name" value=$name>
<INPUT TYPE="HIDDEN" NAME="address" value=$address>
<INPUT TYPE="HIDDEN" NAME="items" VALUE="$items">

<INPUT TYPE="submit" VALUE="SUBMIT" >
</form></center>
</body></html>
PAGE

#end of script
exit
0;





sub outputErr
{
#output error message
print
<<ERRPAGE ;
Content-type: text/html\n\n
<html>
<head>
<title>Error</title>
</head>
@_[0]<br>
Use the browser back button to display the form
</body></html>\n
ERRPAGE
#end script processing
exit
0
}






Main Script:

Code:


#!perl

#Declare the path to the perl interpreter

#include the library for parsing input
use CGI;

#parse the form arguments into variables to hold data
#parse the form arguments
CGI::ReadParse(\%values);

#variables to hold data
$name=$values{
"name"};
$address=$values{
"address"};
$items=$values{
"items"};


#Set cost at 0
$Cost=
0;







#Test whether name entered matches the special
#name if the name matches free poster else no poster

{
if ($name =~ m/elvis/)
{
$result =
"yes";
$match =
1;
}

}

if ($match ==
0)
{
$result =
"no";
}



#Open the file containing the cost data and hold the items in an array
open(FILE,
"prices1.txt") || &outputErr("File access error");;
@list=<FILE>;
close FILE;

#initialise a hash with the item prices from the array
%price=(
"autographs",$list[0],"doll",$list[1],"shoes",$list[2],"cards",$list[3],"jacket",$list[4],"sunglasses",$list[5],"jumpsuit",$list[6]);

#determine the cost of the items
foreach $item (@items)
{
$Cost+=$price{$item};
}




#format the price output
$cost = sprintf(
"%.2f",$cost);

#open file for appending or print error page if access denied
open(OUT,
">>orders.txt") || &outputErr("File Access Denied");
#lock access
flock(OUT,
2);
#write the entered order data to the file
print OUT
"name,$name \n";
print OUT
"address, $address \n";
print OUT
"item, $items \n";
print OUT
"cost, $Cost \n";
print OUT
"poster,$result \n";
print OUT
"********************************************************* \n";
#unlock access
flock(OUT,
8);
#close the file
close(OUT);






#output web page
print
<<PAGE ;
Content-type: text/html \n\n
<html>
<head>
<title>SUCCESS</title>
</head>
<body>
<font color="white"><font size="+5"><font face="vladimir script">
Good Choice</font>
<br>
<br>


<body bgcolor="#000000" >
<font color="white"><font size=+1><i>
Name:$name <br>
<p>
Address: $address <br>
<p>
Purchase{s}: $items<br>
<p>
FREE POSTER? = $result

<font size=+3><br><font size=3>Your choice costs £$Cost</font></br>
<br>
<br>





<center><font size="+5"><font face="vladimir script">Your order has been processed<br>
Thankyou for shopping with us.</center></font></font>

</body></html></i></font><br>


PAGE

#end the script
exit
0;



###############################################start sub##################
sub outputErr
{
#output error message
print
<<ERRPAGE ;
Content-type: text/html\n\n
<html>
<head>
<title>Error</title>
</head>
@_[0]
</body></html>\n
ERRPAGE

#end script processing
exit
0
}



Last edited by:

Steve_Ignorant: Nov 17, 2003, 12:45 PM