Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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)

In reply to (jeffa) Re: Trouble passing parameters into HTML by jeffa
in thread Trouble passing parameters into HTML by Nihil Angst

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found