Gossamer Forum
Home : General : Perl Programming :

2 try not giving up

Quote Reply
2 try not giving up
hi, this is my first try to modify a script,
so i propably make mistakes. i have a shopping-cart script that i am going to incorporate into links. the main script calls
the required paths from a config file. the command looks like:
##########################################################################################---------------------------------------------
&form_parse;
require "configure.cgi";
&configure;

open (TOPHTML, "$tophtml");
@top=<TOPHTML>;
close (TOPHTML);
open (BOTTOMHTML, "$bottomhtml");
@bottom=<BOTTOMHTML>;
close (BOTTOMHTML);


$zqty = $SCANFORM{'qty'};

if ($zqty < 0){
print "Content-type: text/html\n\n";
print "INVALID QUANTITY";
exit;
}

&altercookies;

print "Content-type: text/html\n\n";
print @top;
print "<br>";

print "<div align=\"center\"><center>";
print "<table border=\"0\" width=\"75%\" cellspacing=\"4\" cellpadding=\"0\">";
print "<tr>";
print "<td width=\"72%\" bgcolor=\"#000000\"><font face=\"Arial\" color=\"#FFFFFF\">ITEM DESCRIPTION</td>";
print "<td width=\"7%\" bgcolor=\"#000000\"><font face=\"Arial\" color=\"#FFFFFF\">QTY</font></td>";
print "<td width=\"7%\" bgcolor=\"#000000\"><font face=\"Arial\" color=\"#FFFFFF\">COST</font></td>";
print "<td width=\"7%\" bgcolor=\"#000000\"><font face=\"Arial\" color=\"#FFFFFF\">SUBTOTAL</font></td>";
print "<td width=\"7%\" bgcolor=\"#000000\"><font face=\"Arial\" color=\"#FFFFFF\">MODIFY</font></td>";
print "</tr>";

&process_cookies;

$tax = $total * $salestax;
$grandtotal = $tax + $total;

#print "<tr>";
#print "<td width=\"72%\" bgcolor=\"#000000\"><font face=\"Arial\" color=\"#FFFFFF\">TOTAL</td>";
#print "<td width=\"7%\" bgcolor=\"#FFFFFF\"><font face=\"Arial\" color=\"#FFFFFF\"></font></td>";
#print "<td width=\"7%\" bgcolor=\"#FFFFFF\"><font face=\"Arial\" color=\"#FFFFFF\"></font></td>";
#print "<td width=\"7%\" bgcolor=\"#000000\"><font face=\"Arial\" color=\"#FFFFFF\">$total</font></td>";
#print "<td width=\"7%\" bgcolor=\"#FFFFFF\"><font face=\"Arial\" color=\"#FFFFFF\"></font></td>";
#print "</tr>";
#print "<tr>";
#print "<td width=\"72%\" bgcolor=\"#000000\"><font face=\"Arial\" color=\"#FFFFFF\">TAX</td>";
#print "<td width=\"7%\" bgcolor=\"#FFFFFF\"><font face=\"Arial\" color=\"#FFFFFF\"></font></td>";
#print "<td width=\"7%\" bgcolor=\"#FFFFFF\"><font face=\"Arial\" color=\"#FFFFFF\"></font></td>";
#print "<td width=\"7%\" bgcolor=\"#000000\"><font face=\"Arial\" color=\"#FFFFFF\">$tax</font></td>";
#print "<td width=\"7%\" bgcolor=\"#FFFFFF\"><font face=\"Arial\" color=\"#FFFFFF\"></font></td>";
#print "</tr>";
#print "<tr>";
print "<td width=\"72%\" bgcolor=\"#000000\"><font face=\"Arial\" color=\"#FFFFFF\">GRANDTOTAL</td>";
print "<td width=\"7%\" bgcolor=\"#FFFFFF\"><font face=\"Arial\" color=\"#FFFFFF\"></font></td>";
print "<td width=\"7%\" bgcolor=\"#FFFFFF\"><font face=\"Arial\" color=\"#FFFFFF\"></font></td>";
print "<td width=\"7%\" bgcolor=\"#000000\"><font face=\"Arial\" color=\"#FFFFFF\">$grandtotal</font></td>";
print "<td width=\"7%\" bgcolor=\"#FFFFFF\"><font face=\"Arial\" color=\"#FFFFFF\"></font></td>";
print "</tr>";

print "</table></center></div>";

print "<div align=\"center\"><center>";
print "<form action=\"checkout.cgi\" method=\"post\">";
print "<input type=\"submit\" value=\"CHECKOUT\">";
print "</form></center></div>";

print "<br>";
print @bottom;

############################################################
# PARSE FORM
############################################################
sub altercookies {
print "Content-type: text/html\n";
$buffer =~ s/&qty=$zqty//g;
$buffer =~ s/=/\|/g;
$newitem = "$buffer&qty=$zqty";
print "Set-Cookie:$buffer&qty=$zqty\n";

}
############################################################
# GET COOKIES
############################################################
sub process_cookies{

@nvpairs=split(/;/, $ENV{'HTTP_COOKIE'});
push (@nvpairs,$newitem);

foreach $nvpair (@nvpairs) {

if ($nvpair =~/ /){
($trash,$nvpair) = split(/ /, $nvpair);
($scdata,$scqty) = split(/&qty=/, $nvpair);
$scitem{$scdata} = $scqty;
next;
}

($scdata,$scqty) = split(/&qty=/, $nvpair);
$scitem{$scdata} = $scqty;
}

@nvpairs = ();

foreach $key (keys(%scitem)){
$kaching = "$key&qty|$scitem{$key}";
push (@nvpairs,$kaching);

}

foreach $nvpair (@nvpairs) {
$nvpair =~ s/\|/=/g;
@pairs = split(/&/, $nvpair);
$thisitem = "";
$thisqty = "";
$thiscost = "";
$thisurl = "";
############################################################
# GET ITEM DATA
############################################################
foreach $pair (@pairs){
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
if ($name eq "item"){
$thisitem = $value;
next;
}
if ($name eq " item"){
$thisitem = $value;
next;
}
if ($name eq "qty"){
$thisqty = $value;
next;
}
if ($name eq "cost"){
$thiscost = $value;
next;
}
if ($name eq "url"){
$thisurl = $value;
next;
}
}

if ($thisitem){
$subtotal = $thisqty * $thiscost;

if ($thisqty > 0){

print "<tr>";
print "<form action=\"supermall.cgi\" method=\"post\">";
print "<input type=\"hidden\" name=\"item\" value=\"$thisitem\">";
print "<input type=\"hidden\" name=\"cost\" value=\"$thiscost\">";
print "<input type=\"hidden\" name=\"url\" value=\"$thisurl\">";
print "<td width=\"72%\" bgcolor=\"#000000\"><a href=\"$thisurl\"><font face=\"Arial\" color=\"#FFFFFF\">$thisitem</a><br>";

############################################################
# PRINT ITEM DATA
############################################################
foreach $pair (@pairs){
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
if ($name eq "item"){
next;
}
if ($name eq " item"){
next;
}
if ($name eq "qty"){
next;
}
if ($name eq "cost"){
next;
}
if ($name eq "url"){
next;
}

print "$name = $value<br>";
print "<input type=\"hidden\" name=\"$name\" value=\"$value\">";
}

print "</td>";


print "<td width=\"7%\" bgcolor=\"#000000\"><font face=\"Arial\" color=\"#FFFFFF\"><input type=\"text\" name=\"qty\" size=\"2\"
value=\"$thisqty\"></font></td>";
print "<td width=\"7%\" bgcolor=\"#000000\"><font face=\"Arial\" color=\"#FFFFFF\">$thiscost</font></td>";
print "<td width=\"7%\" bgcolor=\"#000000\"><font face=\"Arial\" color=\"#FFFFFF\">$subtotal</font></td>";
print "<td width=\"7%\" bgcolor=\"#000000\"><font face=\"Arial\" color=\"#FFFFFF\"><input type=\"submit\"
value=\"MODIFY\"></font></td>";
print "</tr>";
print "</form>";
}

############################################################
# PRINT FORM TO SCREEN?
############################################################
$total = $total + $subtotal;
}

}

}
############################################################
# SUBROUTINES
############################################################
sub form_parse {
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@tempairs = split(/&/, $buffer);
foreach $tempairs (@tempairs)
{
($tempname, $tempvalue) = split(/=/, $tempairs);
$tempvalue =~ tr/+/ /;
$tempvalue =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$SCANFORM{$tempname} = $tempvalue;
}}
---------------------------------------------
as you see the script parses input from a form.

the config file looks like this:
---------------------------------------------
sub configure {

$sendmail = '/usr/lib/sendmail';
$billing="webmaster\@blabla.com";
$subject = "blablaCART ONLINE ORDER";

$bottomhtml = "bottom.html";
$tophtml = "top.html";
$thankyouhtml = "thankyou.html";

}
1; # Return true
---------------------------------------------
i want this script to call different config files, by form action.

the form looks like this:
---------------------------------------------
<form method=POST action="blabla.cgi">
<input type="HIDDEN" name="item" value="name of item">
<input type="HIDDEN" name="url" value="url of this items
webpage">
<input type="HIDDEN" name="cost" value="unit cost of item">

INSERT OTHER FORM FIELD DATA HERE (style, color, other
options)

Quantity <INPUT TYPE="TEXT" size="2" NAME="qty" value="1"
ALIGN=left>

<INPUT TYPE="SUBMIT" VALUE="Add to Cart" ALIGN=middle>
</form>
---------------------------------------------
my questions are:
1. what to add to the script to call
config1
config2
config3
if called by different forms
and how to actually make the call from the form to the script. the purpose is to use one script for different shops.
sorry for the long post. but the guy who wrote and sold the script to me is not of any help and rather unfriendly.
hope anyone can help
love+klaus


------------------


------------------