Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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

( #154540=categorized question: print w/ replies, xml ) Need Help??
Contributed by Zombie dima on Mar 27, 2002 at 00:20 UTC
Q&A  > CGI programming


<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"; }

Answer: How do I check if checkbox in the form was checked or not?
contributed by Flame

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.



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...


  • Please read these before you post! —
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (14)
    As of 2014-07-10 21:57 GMT
    Find Nodes?
      Voting Booth?

      When choosing user names for websites, I prefer to use:

      Results (217 votes), past polls