无法通过2个脚本传递参数 - perl

时间:2017-07-26 02:50:06

标签: perl

#!/usr/bin/perl -wT 
use DBI;
use CGI qw(:standard);
use Digest::MD5 qw(md5_hex);
$db="int420_172a14";
$user="int420_172a14";
$passwd="ndWW9855";
$host="db-mysql.zenit";
$connectionInfo="dbi:mysql:$db;$host";
my $q = new CGI;
$submit = param('submit');
@productid = $q->param('productid');
($cookiename, $cookievalue) = split(/=/, $ENV{HTTP_COOKIE});
if ($submit eq "Purchase Item")
{
print "Content-Type:text/html\n\n";
print qq~<html>
<head><title>Checkout Page</title></head>
<body background=/images/back.jpg>
<center><a href=/cgi/stage5.cgi>Click Here To Return To Catalog</a></center>
<center><a href=/>Click Here To Return To Main Page</a></center>
<p>@productid</p>
~;
checkout();
}
elsif ($submit eq "Checkout") {

    &completecheckout();
    }
elsif ($ENV{HTTP_COOKIE} =~ /username/) {
print "Content-Type:text/html\n\n";
catalog();
}
else {
print "Location:http://zenit.senecac.on.ca:20720/cgi/login.cgi\n\n"
}

sub catalog {

$select = qq~select * from catalog~;
$dbh=DBI->connect($connectionInfo,$user,$passwd);
$sth=$dbh->prepare($select);
$sth->execute();
print qq~<html>
<head><title>Shopping Catalog</title></head>
<body background=/images/back.jpg>
<center><h1>Welcome To Our Shopping Catalog</h1>
<h3>Please select a product below</h3></center>
<center><a href=/>Click Here To Return To Main Page</a></center>
<p>$ENV{HTTP_COOKIE}</p>
<table border=2 align=center>
<th>Products</th>
~;
$id = 1;
while (@row=$sth->fetchrow_array())
{
print qq~
<tr>
<td height=100>$row[1]<br>
$row[2]<br>
\$$row[3]<br>
<img src=/images/$row[4] height=100 width=100 alt="Product Image Not Found.">
<form action="" method=POST><input type=checkbox name=productid value="$row[0]">Select Product</td>
</tr>
~;
$id = ++$id;
}
print qq~<tr><td><input type=submit name=submit value="Purchase Item"></td></tr></form></table></body></html>~;
$dbh->disconnect();
}
sub checkout {

my @values = ('yes', 'no');
print "<h3>Product You Are Purchasing</h3><table border=2 align=left>";
foreach (@productid) {
$select = qq~select * from catalog where ID='$_'~;
$dbh=DBI->connect($connectionInfo,$user,$passwd);
$sth=$dbh->prepare($select);
$sth->execute();
while (@row=$sth->fetchrow_array()) {
print qq~
<tr><td>$row[1]<br>$row[2]<br>\$$row[3]<br><img src=/images/$row[4] height=100 width=100 alt="Product Image Not Found."><br></td></tr>~;
$total = $total + $row[3];

}
}
print "<h4>Your total is \$$total</h4>";
print "</table>";
print "<table border=2 align=center>";
print start_form( -name => "complete" ),
'<tr><td>Credit Card:', radio_group(
        -name   => 'radio1',
        -values => ['Visa', 'MasterCard', 'American Express'],
        -columns => 3,
        -rows   => 1,
    ), '</td></tr>',
'<tr><td>Credit Card Number:</td><td>', textfield('cardnum'), '</td></tr>',
    '<tr><td>', submit(-name => 'submit', -value => 'Checkout'), '</tr></td></table>', end_form;
foreach (@productid) {
print start_form(-name=>"complete"),
hidden(
              -name     => "productid",
              -value  => "$_",
              -override => 1
              ),
    print end_form;
}

    }
sub completecheckout {

$uid = $cookievalue;
$cardtype = param('radio1');
$cardnum = param('cardnum');
    foreach $product (@productid) {
        $insert = qq~insert transaction (uid, pid, cctype, ccnum) values ('$uid', '$product', '$cardtype', '$cardnum')~;
        $dbh=DBI->connect($connectionInfo,$user,$passwd);
        $sth=$dbh->prepare($insert);
        $sth->execute();
    }

print header;
print qq~<html><head><title>YAY</title></head><body><p>CHECK MYSQL<p><p>@productid $uid $cardtype $cardnum</p></body></html>~;

    }

我制作了一个脚本,显示SQL表中的产品,并允许用户选择多个产品,然后将每个产品作为具有唯一ID的单独事务上载到事务表。但是,@ productid数组未传递给completecheckout函数,并且没有信息上传到表中。

我意识到脚本有点乱,因为我现在正在处理它,所以请不要对冗余代码发表评论,因为我知道它。

我无法确定表单无法读取隐藏字段的原因。

1 个答案:

答案 0 :(得分:-1)

print "<h4>Your total is \$$total</h4>";
print "</table>";
print "<table border=2 align=center>";
print qq~<form method=post action="">
<tr><td><input type="radio" name="radio1" value="Visa" checked="checked">Visa</td>
<td><input type="radio" name="radio1" value="Mastercard">Mastercard</td>
<td><input type="radio" name="radio1" value="AMEX">Amex</td></tr>
<tr><td>Credit Card Number:</td><td><input type=text name=cardnum></td></tr>
<tr><td><input type=submit name=submit value=Checkout></tr></td></table>~;
foreach (@productid) {
print qq~<input type=hidden name=productid value=$_>~;
}
print qq~</form>~;
    }

从cgi更改为纯HTML工作。它现在传递了隐藏值。