?
Solved

Basic PERL form questions - how to

Posted on 2007-10-02
30
Medium Priority
?
263 Views
Last Modified: 2008-01-09
I have a very basic question, how do i work with forms with perl. I tried google, but i found jack on tutorials with that.

To enter data and store in a variable to be used. Trying to make a car calculator with car loans, etc.

Thanks,

Ryan
0
Comment
  • 18
  • 11
30 Comments
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20003480
Here is my 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/carcalculator.cgi" method="post">

<label for="cost">Vehicle Cost</label><input type="text" name="cost" size="15" />
<label for="payment">Monthly Payment</label><input type="text" name="payment" size="15" />
<label for="interest">Interest Rate</label><input type="text" name="interest" size="15" />
<label for="down">Down Payment</label><input type="text" name="down" size="15" />
<label for="loan">Years in loan</label><input type="text" name="loan" size="15" />


</form>

</body>
</html>


0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 20003603
Have a look at the CGI.pm module (http://perldoc.perl.org/CGI.html).  That's become the de facto standard for CGI in Perl...
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20003628
Okay, thanks. I have this:

http://elan.champlain.edu/~rcoughlin32001/carcalculator.html

How would I go about checking to see if they are blank, the forms and if they are, I use default values?

CODE:

#!/usr/bin/perl
print "Content-type:text/html \n\n";
use CGI qw(:standard);

my $cost       =    param('cost');
my $payment    =    param('payment');
my $interest   =    param('interest');
my $down       =    param('down');
my $loan       =    param('loan');
my $due        =    $cost - $down*1 + $loan*$interest/100;

print $due;


END_______________

I am unsure how to check for that? And how to I create the default values if there is none given.

Thanks,

Ryan
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20003700
If that sounded confusing at all, I should have added to make it more simple:

How do I check to see if each of the form fields are blank and if they are resort to default values that I will set.

I know in PHP you can check for blank fields, but am unsure for Perl.

if(BLANK){

THESE VALUES

}
else{

USER VALUES
}
0
 
LVL 6

Expert Comment

by:nitinsawhney
ID: 20004961
Hi,

You need to create a CGI object before proceeding with using the variables.

#!/usr/bin/perl
use CGI;
my $p = new CGI;

my $cost = $p->param('cost');
my $payment = $p->param('payment');
my $interest = $p->param('interest');
my $down = $p->param('down');
my $loan = $p->param('loan');

print "Content-type:text/html \n\n";

# Do something with the above mentioned variables.

__END__

The beauty of perl is that unlike PHP its so easy to check if the variable is empty or not.

Suppose if interest rate is not provided and we need in that case default to be 12% we can achieve it by.

$interest = ($interest) ? $interest : 12;

So, a simple statement if($cost) will tell you if the cost is blank or it contains any value.

I prefer to write all the html within the qq block so there is no conflicts with the quotation marks.

print qq{
  <html>
    ..
   </html>
};

Remember in CGI file whatever you'll print will go to the browser.

So suppose you calculated the loan in $loanamount and want to show it on the page somewhere just write
print $loanamount; at that location.

With the e.g. I gave above it would be rather simple.
Just put all your html within qq block and whereever applicable use the variable.

print qq{
<html>
...
<body>
...
<p>The loan amount is $loanamount</p>
...
</body>
</html>
};

Hope this is of some help.

Cheers!
0
 
LVL 17

Accepted Solution

by:
mjcoyne earned 2000 total points
ID: 20005265
Default values are created as you render the HTML (which you should let CGI.pm handle as well).  Here's a simple form to get you started.  You can make a much simpler form, of course -- for example, you could leave out the HTML tables and the CSS stuff -- but I wanted to give you as many examples of how to do stuff with CGI.pm as I could:

#!/usr/bin/perl -wT

use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser); # for development only

# Close some potential exploits, comply with taint restrictions
# Change to comply with local setup

$ENV{"PATH"} = '/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

# Close some additional potential exploits.

$CGI::POST_MAX=1024;
$CGI::DISABLE_UPLOADS = 1;

my $q = new CGI;

print $q->header(-type => 'text/html');

print $q->start_html(
    -title => "My test CGI page",
    -meta => {
        keywords => "test, testing, CGI test, Perl",
        description => "A CGI test page -- everyone's got to start somewhere!"
    },
    -style => {-src=>['http://your.url.com/test_page.css']},
    -script => {-language => 'JavaScript1.2', src => 'http://your.url.com/test_page.js'}
);

if ($q->param()) {
    &show_output($q);
} else {
    print $q->start_form;
    print $q->start_table({-class => 'input'});
    print $q->Tr($q->td("Choose"));
    print $q->Tr($q->td($q->popup_menu(
        -name => "Drop_down1",
        -values => ['choice 1', 'choice 2', 'choice 3', 'Select'],
        -default => 'Select',)));
    print $q->end_table;
    print $q->submit(-name=>'submit', -value=>'Submit');
    print $q->end_form;
    print $q->end_html;
}

sub show_output() {

    my ($q) = @_;
    my $Drop_down1_chosen = $q->param('Drop_down1');
   
    print $q->start_table({cellspacing => "2", -class => 'output'});
   
    if ($Drop_down1_chosen eq 'Select') {
        print $q->Tr($q->td({-class => 'Drop_down_answer_label'}, "Answer:"), $q->td({-class => 'Drop_down_answer'}, "none selected"));
    } else {
        print $q->Tr($q->td({-class => 'Drop_down_answer_label'}, "Answer:"), $q->td({-class => 'Drop_down_answer'}, $Drop_down1_chosen));
    }
    print $q->end_table;
    print $q->button(-name=>'Reset', -value=>'Return', -onClick=>'history.go(-1)');
    print $q->end_html;
}

Hope this helps!
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20005882
How would I go about checking to see if the form is not completed then use X values?

And check for each form field?

Thanks for everything and whats above.

Ryan
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20005901
Like you said here:

$interest = ($interest) ? $interest : 12;

That checks to see if it is empty and if not then use that?

Would I replace that with?

my $payment    =    param('payment');
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 20005968
The same way I've done it above, in sub show_output:

# capture the incoming variable(s) selection
    my $Drop_down1_chosen = $q->param('Drop_down1');
    my $Drop_down2_chosen = $q->param('Drop_down2');
    my $Drop_down3_chosen = $q->param('Drop_down3');
        ... etc ...

# test the value of the captured selection, branch appropriately.  Do
# a similar loop for each form variable.

    if ($Drop_down1_chosen eq 'whatever') {
        ... do some stuff ...
    } elsif ($Drop_down1_chosen eq 'something else') { {
        ... do some other stuff ...  # you can have many elsif's
    } else {
         # what to do if the value of $Drop_down1_chosen
         # doesn't match any of the above choices, or if it's
         # empty.
        ... do some other stuff ...
    }
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 20006090
P.S.:  You'll note in my original example, I included 'Select' as a choice, and set it to the default.  Since 'Select' is the default and is not a valid final choice, but rather serves as an instruction to the user, I can tell no choice was made if the param remains equal to 'Select' on submission.  Perhaps then I would reload the form page with a line printed saying something like "Errors found: Please select a choice for State", or whatever thing param(Drop_down1) is designed to capture...

All form fields behave pretty much the same way in CGI.pm.  My example uses drop-down lists, but it could just as easily be radio buttons, hidden fields, text boxes, etc.
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20007511
Alrighty, thank for for that. I have:


#!/usr/bin/perl
print "Content-type:text/html \n\n";
use CGI qw(:standard);

my $cost       =    param('cost');
my $payment    =    param('payment');
my $interest   =    param('interest');
my $down       =    param('down');
my $loan       =    param('loan');
my $due        =    $cost - $down * 1 + $loan * $interest / 100;

my $month      =    $due / 12 * $loan;
print $due;
print "\n\n\n\n\n";
print $month;

And I am trying to put together what you have said above, to see if those values aren't filled in by the client, then use:

Interest rate - 10%
Down payment  20% of total cost
Years in Loan - 5 years

Any suggestions?
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 20008098
I don't see where your form is being printed to be filled in.  And, I'd suggest (as nitinsawhney did above) using CGI in OO mode, as opposed to standard mode.  However, if you just want to know how to set defaults, you can do something like:

my $cost       =    param('cost') || '0.00';
my $payment    =    param('payment') || '0.00';
my $interest   =    param('interest') || '0.10';
my $down       =    param('down') || ($cost * 0.20);
my $loan       =    param('loan') || '5.00';
my $due        =    $cost - $down * 1 + $loan * $interest / 100;

So, for example, the variable $interest will be set to whatever param('interest') returns, unless that's undefined.  If param('interest') is undefined, then $interest is set to 0.10.

Is that what you mean?
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20008141
Yeah, thats what I want to do!

So just to make sure for my sake, sorry.

If not number is given then use say '0.00'?

Thanks!
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 20008764
Yes.  The scalar variable on the left side of the equal sign (e.g. $interest ) will be assigned the value of param('whatever'), unless param('whatever') is undefined (has no value).  If the param('whatever') has no value, the value on the right side of the logical OR (||; e.g. '0.10') is assigned to the scalar variable on the left side of the equal sign.
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20008793
Alrighty, thank you! I never knew you could use the piping symbol for that.

http://elan.champlain.edu/~rcoughlin32001/carcalculator.html

Any idea why my calculations are off?

CODE:

#!/usr/bin/perl
print "Content-type:text/html \n\n";
use CGI qw(:standard);

my $cost       =    param('cost')     || '0.00';
my $payment    =    param('payment')  || '0.00';
my $interest   =    param('interest') || '0.10';
my $down       =    param('down')     || ($cost * 0.20);
my $loan       =    param('loan')     || '5.00';

my $due        =    ($cost - $down) * (1 + $loan * $interest / 100);

my $month      =    $due / (12 * $loan);

# if nothing default values below

# print output of all entered values
# first check if there is no cost, then re display form

if($cost == 0)
{
      my $output = <<_END_;
      
      <form name="calc" action="cgi-bin/carcalculator.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>
      
      _END_
      print $output;
      
}else{

my $output = <<_END_;

<b>Cost:</b> $cost <br />
<b>Payment:</b> $payment <br />
<b>Interest:</b> $interest <br />
<b>Down:</b> $down <br />
<b>Loan:</b> $loan <br />
<br /><br />
<b>Amount due each month:</b> $month

_END_
print $output;
}
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 20009626
Well, for one thing, you're assigning values to your variables based on presumed params() before you've even printed the page to gather these values from the user.  The params can't help but be undefined at the spot you're evaluating them and populating your variables...

Look again at the order of things in the example I gave you:

#!/usr/bin/perl -wT

use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser); # for development only

# Close some potential exploits, comply with taint restrictions
# Change to comply with local setup

$ENV{"PATH"} = '/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

# Close some additional potential exploits.

$CGI::POST_MAX=1024;
$CGI::DISABLE_UPLOADS = 1;

# set up the CGI object
my $q = new CGI;

# print the opening HTML
print $q->header(-type => 'text/html');
print $q->start_html(
   ...etc...
);

# check to see if params have been gathered yet
if ($q->param()) {

# if they have, run the sub show output, passing the CGI object to the sub
    &show_output($q);

} else {
# if no params have been gathered, print the form that gathers the param values
    print $q->start_form;
   ... etc ...
    print $q->end_form;
    print $q->end_html;
}

# this is called if params exists
sub show_output() {
   # grab passed parameters
    my ($q) = @_;

   # assign params to variables, do math, etc..  This is where your
   # my $cost =  param('cost')  ||  '0.00'; and such go, and your math.
    my $Drop_down1_chosen = $q->param('Drop_down1');

   # print results page
    print $q->start_table({cellspacing => "2", -class => 'output'});
   
    if ($Drop_down1_chosen eq 'Select') {
   ...etc...
    print $q->end_html;
}

Please start your script with:

#!/usr/bin/perl -wT

use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser); # for development only

So that 1). You are in taint mode (extremely important, unless you'd like your site to be susceptible to attack), 2). you have warnings and strict enabled (every Perl script pretty much without exception should have these enabled), 3).  you're using CGI.pm in Object Oriented mode, and 4). so script errors during development show up in your browser.

All of the lines in my example script prior to "my $q = new CGI;" are there for a reason, I'd suggest you incorporate them into your script.

BTW, my example is fully functional -- drop it on your server, give it execute permission, and call it up in your browser.


0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20010066
Thank you very much! I am going to go upload this and play with it.

I never knew most of that stuff you were saying, but know i do!

Thanks again,

Ryan
0
 
LVL 17

Assisted Solution

by:mjcoyne
mjcoyne earned 2000 total points
ID: 20013886
Here's a working example to get you started.  Note that there's more work to do on a project like this: checking for invalid input, for example, is a must (what if someone puts "free" in Vehicle Cost?).  But, I think you can play with this example and get it to do what you want.  Once you get it the way you want, delete or comment out the "use CGI::Carp qw(fatalsToBrowser);" line.  Also, the $ENV{"PATH"} line might need to be tweaked to match your server setup -- what OS are you using?

Hope this helps!

#!/usr/bin/perl -wT
use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser); # for development only

# Close some potential exploits, comply with taint restrictions
# Change to comply with local setup

$ENV{"PATH"} = '/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

# Close some additional potential exploits.

$CGI::POST_MAX=1024;
$CGI::DISABLE_UPLOADS = 1;

my $stylesheet =<<END;
body {
    font-family: Arial, Verdana, "Lucida Grande","Arial Unicode MS", sans-serif;
    }
table {
    border-style: dashed;
    border-color: black;
    border-width: 2px;
    }
table.input {
    margin-left: 100px;
    margin-top: 50px;
    }
table.output {
    margin-left: 100px;
    margin-top: 50px;
    }
td.label {
    font-weight: bold;
    text-align: right;
    padding-right: 10px;
    }
td.field {
    padding-left: 10px;
    }
td.header {
    font-weight: bold;
    text-align: center;
    padding: 10px;
    }
td.button {
    text-align: center;
    padding: 10px;
    }
END

my $q = new CGI;

print $q->header(-type => 'text/html');

print $q->start_html(
    -title => "My test CGI page",
    -meta => {
        keywords => "test, testing, CGI test, Perl",
        description => "A CGI test page -- everyone's got to start somewhere!"
    },
    -style => {-code => $stylesheet},
    -script => {-language => 'JavaScript1.2', src => 'http://your.url.com/test_page.js'}
);

if ($q->param()) {
    &show_output($q);
} else {
    print $q->start_form;
    print $q->start_table({-class => 'input'});
    print $q->Tr($q->td({colspan => '2', class => 'header'}, "Vehicle Loan Calculator"));
    print $q->Tr($q->td({width => '150', class => 'label'}, "Vehicle Cost:"),
                 $q->td({width => '220', class => 'field'}, $q->textfield(-name=>'cost', -size => '30')));
    print $q->Tr($q->td({class => 'label'}, "Monthly Payment:"),
                 $q->td({class => 'field'}, $q->textfield(-name=>'payment', -size => '30')));
    print $q->Tr($q->td({class => 'label'}, "Interest Rate:"),
                 $q->td({class => 'field'}, $q->textfield(-name=>'interest', -size => '30')));
    print $q->Tr($q->td({class => 'label'}, "Down Payment:"),
                 $q->td({class => 'field'}, $q->textfield(-name=>'down', -size => '30')));
    print $q->Tr($q->td({class => 'label'}, "Years in Loan:"),
                 $q->td({class => 'field'}, $q->textfield(-name=>'loan', -size => '30')));
    print $q->Tr($q->td({colspan => '2', class => 'button'}, $q->submit(-name=>'submit', -value=>'Submit')));
    print $q->end_table;
    print $q->end_form;
    print $q->end_html;
}

sub show_output() {
    my ($q) = @_;
    my $cost = $q->param('cost') || '0.00';
    my $payment = $q->param('payment') || '0.00';
    my $interest = $q->param('interest') || '0.10';
    my $down = $q->param('down') || ($cost * 0.20);
    my $loan = $q->param('loan') || '5.00';
    my $due;

    if ($interest > 1) {
        $due = ($cost - $down) * (1 + $loan * $interest / 100);
        $interest = sprintf ("%.2f", $interest) . "%";
    } else {
        $due = ($cost - $down) * (1 + $loan * $interest);
        $interest = sprintf ("%.2f", $interest * 100) . "%";
    }

    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";

    print $q->start_table({-class => 'output'});
    print $q->Tr($q->td({colspan => '2', class => 'header'}, "Loan Calculator Results"));
    print $q->Tr($q->td({width => '150', class => 'label'}, "Cost:"),
                 $q->td({width => '220', class => 'field'}, $cost));
    print $q->Tr($q->td({class => 'label'}, "Payment:"),
                 $q->td({class => 'field'}, $payment));
    print $q->Tr($q->td({class => 'label'}, "Interest:"),
                 $q->td({class => 'field'}, $interest));
    print $q->Tr($q->td({class => 'label'}, "Down:"),
                 $q->td({class => 'field'}, $down));
    print $q->Tr($q->td({class => 'label'}, "Loan:"),
                 $q->td({class => 'field'}, $loan));
    print $q->Tr($q->td({class => 'label'}, "Amount per month:"),
                 $q->td({class => 'field'}, $month));
    print $q->Tr($q->td({colspan => '2', class => 'button'}, $q->button(-name=>'Reset', -value=>'Return', -onClick=>'history.go(-1)')));
    print $q->end_table;
    print $q->end_html;
}

sub commify {
    my $input = shift;
    $input = reverse $input;
    $input =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
    return reverse $input;
}
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20022339
Hey man, I have:

http://elan.champlain.edu/~rcoughlin32001/cgi-bin/carcalculator-ee.cgi

And i get:

Software error:

Bareword "top" not allowed while "strict subs" in use at carcalculator-ee.cgi line 139.
Execution of carcalculator-ee.cgi aborted due to compilation errors.

For help, please send mail to the webmaster (root@elan.champlain.edu), giving this error message and the time and date of the error.


That error, any idea on how to fix it?

And I am just new at Perl and I am wondering if there is an easier way to write that code? I see some extra stuff in there like:

"%.2f"
sprintf
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20022345
I fixed it, I pasted the "back to top text on it, haahh.

Ryan
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20022369
I am also wondering what does this line do:

my $q = new CGI;
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20022383
CODE:

print $q->start_html(
    -title => "My test CGI page",
    -meta => {
        keywords => "test, testing, CGI test, Perl",
        description => "A CGI test page -- everyone's got to start somewhere!"
    },
    -style => {-code => $stylesheet},
    -script => {-language => 'JavaScript1.2', src => 'http://your.url.com/test_page.js'}
);

This is interesting, i never knew you could print out HTML like that with using Perl

Ryan
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 20023665
my $q = new CGI; sets up a new CGI object called $q.  We use this object throughout the code (all the "$q->something" lines).

The "sprintf ("%.2f", $number)" lines take the value held in $number and round off to the indicated decimal places (in this case, 2).  So, if $number is 1.237, 1.24 is returned, and if number is 1, 1.00 is returned.  In these examples, I'm just reformatting the numbers so they're output as currency on submit.

Read more about the very versitile sprintf at http://perldoc.perl.org/functions/sprintf.html.

I actually meant to clean up the start_html call (you're not using meta tags or javascript, for example) but neglected to do so.  Still, there's no harm -- the code runs perfectly even with it there -- and if you ever need meta tags or javascript, now you know how to do it...
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20028612
Oh wow! Thank you for all of this! I am learning alot.

I will keep this for later use like you said above. I appreciate you helping me out.

Ryan
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20030161
Hey, I am wondering with the full working code you gave me, is it possible to set it u through a HTML form, then use the action xxx.cgi?

Would I take out this:

if ($q->param()) {
    &show_output($q);
} else {
    print $q->start_form;
    print $q->start_table({-class => 'input'});
    print $q->Tr($q->td({colspan => '2', class => 'header'}, "Vehicle Loan Calculator"));
    print $q->Tr($q->td({width => '150', class => 'label'}, "Vehicle Cost:"),
                 $q->td({width => '220', class => 'field'}, $q->textfield(-name=>'cost', -size => '30')));
    print $q->Tr($q->td({class => 'label'}, "Monthly Payment:"),
                 $q->td({class => 'field'}, $q->textfield(-name=>'payment', -size => '30')));
    print $q->Tr($q->td({class => 'label'}, "Interest Rate:"),
                 $q->td({class => 'field'}, $q->textfield(-name=>'interest', -size => '30')));
    print $q->Tr($q->td({class => 'label'}, "Down Payment:"),
                 $q->td({class => 'field'}, $q->textfield(-name=>'down', -size => '30')));
    print $q->Tr($q->td({class => 'label'}, "Years in Loan:"),
                 $q->td({class => 'field'}, $q->textfield(-name=>'loan', -size => '30')));
    print $q->Tr($q->td({colspan => '2', class => 'button'}, $q->submit(-name=>'submit', -value=>'Submit')));
    print $q->end_table;
    print $q->end_form;
    print $q->end_html;
}

sub show_output() {

Then just make the action xxx.cgi?

Ryan
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20030234
Hey again, I was trying to strip it down and try something for my own sake before adding in the others, going step by step.

I have:

#!/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/carcalculator-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>

Any ideas on how to fix thaT?

Thanks,

Ryan
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 20031938
I'm not sure what you're trying to accomplish.
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20032316
It wont print out any results.

Ryan
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 20033015
Let's close this question out, and move the next part to a new question -- the CGI form example I gave you works perfectly and thus answers this question -- further code modification is more appropriately handled in a different question.

...and I'm still unclear on what it is you're trying to "improve"...:)
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20034219
Alrighty, i will post another one.

Thanks,

Ryan
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question