Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

How do I check if checkbox in the form was checked or not?

by dima (Initiate)
on Mar 27, 2002 at 00:20 UTC ( [id://154540]=perlquestion: print w/replies, xml ) Need Help??

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

<input type="checkbox" name="checkbox" value="on" checked>

When I print the value of checkbox it prints blank instead of on, and thats why pearl code below always goes into else statement. What should I do so that I can check if the checkbox is marked or not?????? Thanks a lot!!!!

if ($field{'checkbox'}) { # checkbox was checked $chkbutton1 = "Yes send me news letter"; } else { # checkbox left unchecked $chkbutton1 = "No don't send me news letter"; }

Replies are listed 'Best First'.
Re: How do I check if checkbox in the form was checked or not?
by cLive ;-) (Prior) on Mar 21, 2002 at 18:55 UTC
    #!/usr/bin/perl -w use strict; my $q = CGI->new(); if ($q->param('check_box_name') { # checkbox was checked } else { # checkbox left unchecked }

    cLive ;-)

Re: How do I check if checkbox in the form was checked or not?
by Flame (Deacon) on Mar 27, 2002 at 00:42 UTC
    First, allways "use CGI;" to get values, it's simple, safe, and effective.

    Second, you need to make sure you specify a value for the checkbox if you really want to be able to see if it is checked.

    Eg: <INPUT TYPE=CHECKBOX NAME="MyBOX" VALUE="on">

    Code:

    if($cgi->param('MyBOX') eq 'on'){ #Do whatever }else{ #... }

    This should also work:

    if($cgi->param('MyBOX')){ #Do whatever }else{ #... }

    But the first is a little more paranoid.

    Hope this helps...

Re: How do I check if checkbox in the form was checked or not?
by taint (Chaplain) on Jun 13, 2018 at 06:19 UTC
    I think this might need an update:

    In order to get the above code to work now days. The following addition to the use statement will be required:

    use strict; use CGI qw/standard/;

Re: How do I check if checkbox in the form was checked or not?
by Juerd (Abbot) on Mar 21, 2002 at 06:22 UTC
    When it's checked, its value is sent. When it's not, it's not.
Re: How do I check if checkbox in the form was checked or not?
by meghavarshney (Novice) on May 23, 2013 at 04:56 UTC
    meghs

    Originally posted as a Categorized Answer.

Re: Q2: How do I check if checkbox in the form was checked or not?
by Anonymous Monk on Oct 22, 2003 at 17:47 UTC
    Yes but what if CGI.pm is not installed on your host?

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-03-28 08:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found