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

Very little CGI

by nathanvit (Beadle)
on May 10, 2003 at 16:25 UTC ( [id://257137]=perlquestion: print w/replies, xml ) Need Help??

nathanvit has asked for the wisdom of the Perl Monks concerning the following question:

Hello, i'm starting now to write CGI and i'm also a perl newbie so...
i made a html form:
<form>action="http://argo.linux.sys/~nathanvi/cgi-bin/distro.pl" metho +d="POST"> Quale e' il tuo nome e cognome?<input type="text" name="nome" size="60 +" maxlength="59"> <br> Come hai conosciuto linux?<textarea name="conosciuto" rows="10" cols=" +40"></textarea><br> Quale distro usi?<br> <select name="distro" size="3"> <option value="mdk" label="mandrake">Mandrake</option> <option value="debian" label="debian">Debian</option> <option value="slack" label="slackware">Slackware</option> </select><br> <input name="Spedisci" value="Invia" type="submit"> </form>
and i wrote a cgi which can write the name of the user... The code is:
#!/usr/bin/perl use strict; my @campi; print "Content-type:text/html\n\n"; print "<html><head><title>Distribuzione usata</title></head>\n"; print "<body>\n"; @campi = split(/\&/,$ENV{'QUERY_STRING'}); print "Il tuo nome e cognome e': $campi[0] <br>"; print "</body>\n"; print "</html>\n";
When i press the post buttom it doesn't give me an error but it doesn't print the variable $campi[0].
Any suggestions?

-­--
os: Linux Mandrake 9.0
perl: obtained with OS

Replies are listed 'Best First'.
(jeffa) Re: Very little CGI
by jeffa (Bishop) on May 10, 2003 at 16:38 UTC
    use CGI.pm !!!
    #!/usr/bin/perl -T use strict; use warnings; use CGI qw(:standard); my %label = ( mdk => 'Mandrake', debian => 'Debian', slack => 'Slackware', red => 'Red Hat!!', ); print header,start_html,start_form, "Quale e' il tuo nome e cognome?", textfield('nome'), br(), 'Come hai conosciuto linux?', textarea('conosciuto'), br(), 'Quale distro usi?', br(), popup_menu( -name => 'distro', -values => [keys %label], -labels => \%label, ), br(), submit(-name=>'Spedisci',-value=>'Invia'), end_form, ; if (param('Spedisci')) { print pre( 'nome = ', param('nome'), "\n", 'conosciuto = ', param('conosciuto'), "\n", 'distro = ', param('distro'), "\n", ); } print end_html;
    I recommend that you check out Ovid's Web Programming with Perl in the meantime. :)

    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)
    
      Really thanks jeffa but the problem is that i'm a really newbie and i'm learning yet perl so it very hard for me learn also CGI module.
      i'd like (now) to write CGI tag by tag as in my example :-( and then whem i will be more skilled write in CGI module style... So do you now where is the problem. Please.

      -­--
      os: Linux Mandrake 9.0
      perl: obtained with OS

        Ok, so you don't have to use CGI.pm to write tags, but if you don't use CGI.pm to parse the incoming parameters, you are asking for trouble. At the very least, try something like this in your distro.pl file:
        use strict; use warnings; use CGI qw(:standard); print header,start_html; if (param('Spedisci')) { print pre( 'nome = ', param('nome'), "\n", 'conosciuto = ', param('conosciuto'), "\n", 'distro = ', param('distro'), "\n", ); } else { print "nothing submitted!"; } print end_html;
        Follow that link to Ovid's web course. Take the time to read it -- it's time well invested!

        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)
        

        Ciao nathanvit,
        please follow jeffa's advice and don't follow what is written in the online book linked from your home page :)

        Parsing form data is not easy as it may seem and the best solution is to use CGI.pm. Its usage isn't very different from the one explained in the online book for the Mcgi module. update: keep that book, but read section 14.4, where the author describes CGI.pm.

        The problem you encountered is that you are trying to read form data from $ENV{QUERY_STRING} that is filled only if you use method GET in your HTML form. When you use the POST method, data will arrive on standard input. So you have three options: change method from POST to GET or read form data from STDIN or use CGI.pm :)

        Ciao, Valerio

Re: Very little CGI
by demerphq (Chancellor) on May 10, 2003 at 17:48 UTC

    perl: obtained with OS

    Weird, ive never seen perl -v print that out.... :-)


    ---
    demerphq

    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
Re: Very little CGI
by Mr. Muskrat (Canon) on May 12, 2003 at 13:55 UTC
    Check the form itself... It may just be a cut and paste error but you posted the following:
    <form>action="http://argo.linux.sys/~nathanvi/cgi-bin/distro.pl" method="POST">
    Try changing that first > to a space and see what happens. :-)

Log In?
Username:
Password:

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

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

    No recent polls found