Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
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 ( #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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://154540]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2018-07-21 19:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (450 votes). Check out past polls.

    Notices?