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

How to extract a form data in cgi perl

by ghosh123 (Monk)
on May 02, 2012 at 16:16 UTC ( [id://968479]=perlquestion: print w/replies, xml ) Need Help??

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


Hi Monk,
I have a cgi perl script in which I have the following HTML form data embedded. Please let me know how can I retrieve the "StatusFilterSelect" value through cgi object

<form name="frmStatusFilter" method="GET" action="http://webserver.com +:8010/cgi-bin/mycgi.pl"> Choose Filter:<select name="StatusFilterSelect" onChange="frmStatusFil +ter.submit()"> <option>Show with status</option> <option>Show all</option> </select> </form>

I am trying with the following, but not working Please help. Thanks in advance.

my $q = new CGI; my $status = $q->param('StatusFilterSelect'); $q->html("status is $status"); ## not showing any value

$status is not showing anything. Please assume I am not making any syntax error. Is this how to extract the value in chi perl. Is my approach right?

Replies are listed 'Best First'.
Re: How to extract a form data in cgi perl
by Corion (Patriarch) on May 02, 2012 at 16:19 UTC

    What is the value of the options? They have no value= attribute.

      You don't have to have values for options. On form submit the value for the element is the string value between the opening and closing option tags. So that's not it.

      If you take the action out and leave everything else intact, you can see the GET values in the URL bar in your browser:

      <form name="frmStatusFilter" method="GET"> Choose Filter:<select name="StatusFilterSelect" onChange="frmStatusFil +ter.submit()"> <option>Show with status</option> <option>Show all</option> </select> </form>
      When I do that, I see the value set. That means your CGI code had something wrong. I created a file called mycgi.pl and put it in the same directory as the HTML file, resulting in this for the form definition:
      <form name="frmStatusFilter" method="GET" action="mycgi.pl">

      You would, of course, leave yours as the same, since you have a specific CGI URL already defined.

      Then I worked on the contents of mycgi.pl and added some missing CGI method calls:

      my $q = new CGI; my $status = $q->param('StatusFilterSelect'); print $q->header(), $q->start_html('Status Report'), $q->h1('Status Report'), $q->p("status is $status"), $q->end_html();

      You left off the header, which is very important to tell the browser that what's coming back is an HTML page. You also want the start_html to emit the html, head, and body tags. The h1 is purely optional, the p is for your data, and the end_html closes off the body and html tags to make everything neat, tidy, and validateable.

Re: How to extract a form data in cgi perl
by thomas895 (Deacon) on May 03, 2012 at 00:16 UTC

    There is no CGI->html() function, as far as I know. You would have to do what Sinistral did, with sending headers and the complete HTML document. Also, when writing web applications, a good tip is to use CGI::Carp, like so:

    use strict; use warnings; use CGI::Carp qw( fatalsToBrowser ); #Comment out when you release. use CGI; my $q = new CGI; print $q->header( "text/html" ), $q->start_html( "Status" ), $q->h1( "Status is ". $q->param( "StatusFilterSelect" ) ), $cgi->end_html();
    ~Thomas~ I believe that the source code to life is written in Perl :-)
Re: How to extract a form data in cgi perl
by Anonymous Monk on May 03, 2012 at 03:53 UTC

    status is not showing anything

    That is because you're making stuff up :)

    As CGI (and Ovids CGI Course ) documents, no CGI functions print stuff, they just return values, you have to print stuff yourself

    $ perl -MCGI -le " CGI->html(q/hello/) " $ perl -MCGI -le " print CGI->html(q/hello/) " <html>hello</html>
Re: How to extract a form data in cgi perl
by mendeepak (Scribe) on May 05, 2012 at 05:47 UTC

    i don't think the $q->html("status is $status"); ## not showing any value will print go for this instead and check print $q->html("status is $status"); ## not showing any value

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-09-18 22:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (25 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.