Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

(jeffa) Re: Trouble passing parameters into HTML

by jeffa (Bishop)
on Aug 16, 2002 at 15:11 UTC ( [id://190664]=note: print w/replies, xml ) Need Help??


in reply to Trouble passing parameters into HTML

First, switching from TripodCGI.pm to CGI.pm is a breeze. Just replace this:
require TripodCGI; $CGI = TripodCGI;
with this:
use CGI; $CGI = CGI->new();
Now, you really should also be using strict.pm as well, which (among other Good Things™) forces you to declare your variables. tye has given the Monastery an excellent writeup on strict.pm - read it!

A couple of the cool features of CGI.pm are it's function interface (importing it's subroutines right into your script so you just type param() instead of $CGI->param()) and it's methods for generating HTML, such as p(), h2(), and form elements. So, with that in mind - here is a rewrite of your script:

use CGI qw(:standard); use strict; print header,start_html('CGI Test'); unless (param('go')) { print start_form, table( Tr( td('First Name:'), td(textfield('fname')), ), Tr( td('Last Name:'), td(textfield('lname')), ), Tr( td('Your age:'), td(textfield('age')), ), ), submit('go'), end_form, ; } else { my $fname = param('fname'); my $lname = param('lname'); my $age = param('age'); print h2('Welcome!'), p(qq|We're glad to see you $fname, members of the $lname family are always welcome here. It's hard to believe that you're already $age years old. My how time flies.|), end_html ; }
So, how does it work? First, we use CGI.pm - that qw(:standard) bit is just a fancy way of telling CGI.pm that we want to be lazy and import some standard functions like param(),header(),h2(), etc. into our script's namespace. Next, we check for a paramater named 'go' - this is going to be the name of the submit button. If it is present, then the user submitted the form. If not, then this is the first time the user has loaded the script, so we need to give them a form to submit. This technique allows you to replace having to use one file to present a form and another file to show the results, and the script is called a reentering script, because it can do one of two or more states.

And now, a bonus - the same script using HTML::Template:

use CGI; use HTML::Template; use strict; my $q = CGI->new(); my $html = do {local $/;<DATA>}; my $tmpl = HTML::Template->new( scalarref => \$html, associate => $q, ); print $q->header,$tmpl->output(); __DATA__ <html> <head> <title>CGI Test</title> </head> <body> <tmpl_unless go> <form method="post"> <table> <tr> <td>First Name:</td> <td><input type="text" name="fname" /></td> </tr> <tr> <td>Last Name:</td> <td><input type="text" name="lname" /></td> </tr> <tr> <td>Your age:</td> <td><input type="text" name="age" /></td> </tr> </table> <input type="submit" name="go" value="go" /> </form> <tmpl_else> <h2>Welcome!</h2> <p> We're glad to see you <tmpl_var fname>, members of the <tmpl_var lname> family are always welcome here. It's hard to believe that you're already <tmpl_var age> years old. My how time flies. </p> </tmpl_unless> </body> </html>
I won't explain how this works here, you will have to go and read my HTML::Template Tutorial. Also, you will need to install HTML::Template - read tachyon's A Guide to Installing Modules to find out how to install a CPAN module when you do not have root access the box. Good luck! :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://190664]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-23 18:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found