Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

HTML forms

by Anonymous Monk
on Dec 27, 2003 at 16:30 UTC ( #317196=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Under CGI.pm, is it possible to print HTML forms using straight HTML? Instead of print Tr(td(textbox(->..) do something like:
print <<"END"; <table><tr><td>Name: </td> <td><input type="text" name="name" size="4 +0" /></td></tr></table> END
And still use the textbox name as you normally would (my $data = param('name');) ? What I'm really trying to ask is, is it possible to paste straight HTML forms and have them work?

Comment on HTML forms
Download Code
Re: HTML forms
by davido (Archbishop) on Dec 27, 2003 at 16:34 UTC
    Yes. It is. Just DWYM and it will work. HERE docs work fine, intermingled with output from CGI.pm.

    Just be sure that the field names you use in your HERE doc are the same ones you look for via $q->param('...'). There is no difference between the output of the "CGI.pm HTML shortcuts" and the output of printing a HERE doc.

    The strongest case for using the CGI.pm HTML shortcuts is that it adds some degree of compiletime syntax checking to your HTML. But frankly, I often find them awkward, and often find myself using CGI.pm only for it's input parameter handling, handling uploads, and for the header() method. YMMV.


    Dave

      Hi davido,

      But frankly, I often find them awkward, and often find myself using CGI.pm only for it's input parameter handling, handling uploads, and for the header() method. YMMV.
      That's exactly what I'm using CGI.pm for. It kinda makes me wonder if it's because I've yet to fully learn its functionality with respect to outputting HTML. Ah, now I know I'm not alone...:)

      The reasons I shun CGI.pm for its HTML capability are 1) I don't want to have to learn the additional syntax to do HTML and 2) I'm already very familiar with HTML and want to make use of that knowledge.

      Lately, I discovered HTML::Template through this community and have since used it for managing content. I find it a really neat solution to marry the two skills of perl CGI and HTML.

Re: HTML forms
by b10m (Vicar) on Dec 27, 2003 at 16:38 UTC

    Why don't you just try and see wheter it works? But yes, you can print straight HTML.

    #!/usr/bin/perl -w use strict; use CGI qw/:standard/; my $q = new CGI; print header, start_html; if($q->param('name')) { print h1("You becha this works.!"), p("You entered: ". $q->param('n +ame')); } else { print<<END <form action="test.cgi" method="POST"> <table> <tr> <td><input type="text" name="name"></td> </tr> </table> <input type="submit"> </form> END ; } print end_html;

    update: Removed <readmore> tags, for there wasn't that much more to read.

    --
    b10m
Re: HTML forms
by pg (Canon) on Dec 27, 2003 at 17:00 UTC

    Don't be scared by those core modules. Although CGI.pm probably has more function calls than most of other core modules, and tend to scare people, it is actually nothing more than a bunch of functions, which take parameters and generate HTML format strings. Just as you are able to concatnate HTML strings returned from various CGI.pm function calls, it is quite obvious that you can mix your own HTML format string with them.

Re: HTML forms
by CountZero (Bishop) on Dec 27, 2003 at 20:19 UTC
    Yes, sure you can roll your own HTML.

    If you do that, you do not need the HTML functions of CGI.pm and you can replace CGI.pm by CGI::Simple (less overhead and faster).

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      I took your advice and plugged CGI::Simple::Standard into a current project and found it consistently about 1/10 to 2/10 of a second slower than standard CGI. Current versions of both modules on activestate. Changing one line changed the output from times():
      #use CGI qw(Vars param header redirect); use CGI::Simple::Standard qw(Vars param header redirect);

      --
      perl -MO=Deparse -e"u j t S n a t o e h r , e p l r a h k c r e"

        Very interesting: CGI::Simple states to be faster than CGI. So either this claim is not true or there is something else: perhaps CGI is preloaded by the server and CGI::Simple needs to be loaded again and again at the start of the script? If I have some time to spare during the next weekend, I might try some tests myself.

        Update: I now only see that you are using the functional interface to CGI::Simple (i.e. CGI::Simple::Standard) and that the claims to be faster only relate to the OO-interface.

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: HTML forms
by CharlesClarkson (Curate) on Dec 28, 2003 at 01:50 UTC

    I would advise you avoid embedded mark up in your scripts if you can. I realize that some situations make this impossible, but one of the hardest things for many web designers to easily edit is perl code.

    I prefer HTML::Template. YMMV. Search CPAN for templates. If you are going to share your code with others or if anyone else is involved with the design of your site, they will thank you for not embedding mark up.

    HTH,
    Charles
Re: HTML forms
by esharris (Monk) on Dec 28, 2003 at 08:58 UTC
    I started writing CGI scipts using straight HTML. But now I am experimenting with using CGI.pm. I suggest buying Lincoln Stein's book. There is a methodology behind CGI.pm that should make CGI scripts easier to build and maintain. The thing that sold me is the sticky forms. If I want to let the user hit a submit button that does something and restores that form without resetting the fields to its default values, I can do it with less explicit code. Instead of reinventing this mechanism, I can use a common convention. One side benefit and that the CGI functions often produces better output. Using straight HTML, I was able to introduce some javascript. Using CGI functions, I was able to do the same thing. However, the CGI functions also added code to comment out the javascript within older browsers. Another side benefit is that the code needed to build tables is more compact when you use CGI functions. I have some colleagues who applaud the move. Conversely, there are a few who make it sound like CGI is going to eventually bite me. We'll see.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://317196]
Approved by Corion
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2014-09-30 22:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (385 votes), past polls