catonthecouchproductions
asked on
No output with form
If you look at my code below and if you click the link to test it out, it will not output anything and I am not sure why..any suggestions?
Thanks,
Ryan
#!/usr/bin/perl
use CGI qw(:standard);
print "Content-type:text/html \n\n";
# || symbol used for use param or 0.00 instead of using if else
my $cost = param('cost') || '0.00'; # cost of car
my $payment = param('payment') || '0.00'; # payment
my $interest = param('interest') || '0.10'; # interest rate
my $down = param('down') || ($cost * 0.20); # down payment on car
my $loan = param('loan') || '5.00'; # loan in years
my $due; # amount due
# calculations for figuring loan
if ($interest > 1) {
$due = ($cost - $down) * (1 + $loan * $interest / 100);
# sprintf() used for decimal value - gets rid of trailing number points
# http://perldoc.perl.org/functions/sprintf.html
$interest = sprintf ("%.2f", $interest) . "%";
} else {
$due = ($cost - $down) * (1 + $loan * $interest);
$interest = sprintf ("%.2f", $interest * 100) . "%";
}
# commify used for creating commas commify()
# http://www.perturb.org/display/entry/562/
my $month = "\$" . commify(sprintf ("%.2f", ($due / (12 * $loan))));
$cost = "\$" . commify(sprintf ("%.2f", $cost));
$payment = "\$" . commify(sprintf ("%.2f", $payment));
$down = "\$" . commify(sprintf ("%.2f", $down));
$loan = $loan . " years"; # . " years"; used for concating and adding on X years
# start output
my $output = <<_END_;
<table width="100%" border="1" cellpadding="5">
<tr>
<td width="150">Cost:</td>
<td>$cost</td>
</tr>
<tr>
<td width="150">Payment</td>
<td>$payment</td>
</tr>
<tr>
<td width="150">Interest</td>
<td>$interest</td>
</tr>
<tr>
<td width="150">Down</td>
<td>$down</td>
</tr>
<tr>
<td width="150">Loan</td>
<td>$loan</td>
</tr>
<tr>
<td width="150">Per Month:</td>
<td>$month</td>
</tr>
</table>
_END_
print $output;
And if you go to:
http://elan.champlain.edu/~rcoughlin32001/carcalculator.html
You will see a blank if you fill it out:
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Car Calculator</title>
</head>
<body>
<form name="calc" action="cgi-bin/carcalcula tor-ee.cgi " method="post">
<label for="cost">Vehicle Cost:</label><input type="text" name="cost" size="15" /><br />
<label for="payment">Monthly Payment:</label><input type="text" name="payment" size="15" /><br />
<label for="interest">Interest Rate:</label><input type="text" name="interest" size="15" /><br />
<label for="down">Down Payment:</label><input type="text" name="down" size="15" /><br />
<label for="loan">Years in loan:</label><input type="text" name="loan" size="15" /><br />
<input type="submit" name="submit" value="Caclulate" />
</form>
</body>
</html>
END CODE
Thanks,
Ryan
#!/usr/bin/perl
use CGI qw(:standard);
print "Content-type:text/html \n\n";
# || symbol used for use param or 0.00 instead of using if else
my $cost = param('cost') || '0.00'; # cost of car
my $payment = param('payment') || '0.00'; # payment
my $interest = param('interest') || '0.10'; # interest rate
my $down = param('down') || ($cost * 0.20); # down payment on car
my $loan = param('loan') || '5.00'; # loan in years
my $due; # amount due
# calculations for figuring loan
if ($interest > 1) {
$due = ($cost - $down) * (1 + $loan * $interest / 100);
# sprintf() used for decimal value - gets rid of trailing number points
# http://perldoc.perl.org/functions/sprintf.html
$interest = sprintf ("%.2f", $interest) . "%";
} else {
$due = ($cost - $down) * (1 + $loan * $interest);
$interest = sprintf ("%.2f", $interest * 100) . "%";
}
# commify used for creating commas commify()
# http://www.perturb.org/display/entry/562/
my $month = "\$" . commify(sprintf ("%.2f", ($due / (12 * $loan))));
$cost = "\$" . commify(sprintf ("%.2f", $cost));
$payment = "\$" . commify(sprintf ("%.2f", $payment));
$down = "\$" . commify(sprintf ("%.2f", $down));
$loan = $loan . " years"; # . " years"; used for concating and adding on X years
# start output
my $output = <<_END_;
<table width="100%" border="1" cellpadding="5">
<tr>
<td width="150">Cost:</td>
<td>$cost</td>
</tr>
<tr>
<td width="150">Payment</td>
<td>$payment</td>
</tr>
<tr>
<td width="150">Interest</td>
<td>$interest</td>
</tr>
<tr>
<td width="150">Down</td>
<td>$down</td>
</tr>
<tr>
<td width="150">Loan</td>
<td>$loan</td>
</tr>
<tr>
<td width="150">Per Month:</td>
<td>$month</td>
</tr>
</table>
_END_
print $output;
And if you go to:
http://elan.champlain.edu/~rcoughlin32001/carcalculator.html
You will see a blank if you fill it out:
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Car Calculator</title>
</head>
<body>
<form name="calc" action="cgi-bin/carcalcula
<label for="cost">Vehicle Cost:</label><input type="text" name="cost" size="15" /><br />
<label for="payment">Monthly Payment:</label><input type="text" name="payment" size="15" /><br />
<label for="interest">Interest Rate:</label><input type="text" name="interest" size="15" /><br />
<label for="down">Down Payment:</label><input type="text" name="down" size="15" /><br />
<label for="loan">Years in loan:</label><input type="text" name="loan" size="15" /><br />
<input type="submit" name="submit" value="Caclulate" />
</form>
</body>
</html>
END CODE
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thank you so much!
ASKER
Could you break down that last segment of code for I can see what each means? Want to learn some code.
Ryan
Ryan
it's using regular expressions to match groups of digits and add in commas. google perl regex for some tutorials... there is far more to learn about regex than i could describe here... :) far more than i even know myself.
There are entire books on the subject in fact. Have fun! :)
Glad i was able to help!
There are entire books on the subject in fact. Have fun! :)
Glad i was able to help!
ASKER
Thank you very much for all of this!
Ryan
Ryan
_END_
print $output;
sub commify {
my $input = shift;
$input = reverse $input;
$input =~ s<(\d\d\d)(?=\d)(?!\d*\.)>
return reverse $input;
}
oh and here it is working:
http://postpoems.com/ttt.html