Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

How to check if checkbox is checked

shaolinf (Programmer) (OP)
21 Nov 07 21:48
How do I check if my check box is checked ?

<input type="checkbox" name="remember" value="on"> Remember Me<br>

if($cgi->param($formdata['remember']) eq 'on'){
{
    print "checkbox selected";
} else {
    print "checkbox  selected";
}

that doesn't work, even if I change the condition to:

if($cgi->param('$formdata['remember']') eq 'on'){
chrismassey (Programmer)
21 Nov 07 22:19

CODE

#HTML
<input type="checkbox" name="check" value="0">
<input type="checkbox" name="check" value="1">

#PERL
print "Content-type:text/html\n\n";
my @checks = param('check');
foreach (@check) {
     if ($_ == "0") {
          print "Checkbox OFF\n\n";
     }
     if ($_ == "1"_ {
          print "Checkbox ON\n\n";
     }
}

Quote:


print "checkbox selected";
} else {
print "checkbox  selected";

Either way the condition goes, it will print "checkbox selected". Did you mean - else { print "checkbox not selected";

Chris
chrismassey (Programmer)
21 Nov 07 22:24
Syntax Error:

if ($_ == "1") {
shaolinf (Programmer) (OP)
21 Nov 07 23:05
Thanks. Yes that is what I meant, selected and not selected. I've tried that code but it doesn't work. All i get is a white page:

HTML
<input type="checkbox" name="remember" value="on">

PERL/CGI
my @checks = param($formdata{'usrname'});
foreach (@check) {
     if ($formdata{'usrname'} == "on") {
          print "Checkbox OFF\n\n";
     }
}
KevinADC (TechnicalUser)
21 Nov 07 23:25
#HTML
<input type="checkbox" name="check" value="foo">
<input type="checkbox" name="check" value="bar">

#PERL

CODE

use strict;
use warnings;
use CGI::Carp qw/fatalsToBrowser/;
use CGI qw/:standard/;
print header, start_html;
my @checks = param('check');
foreach (@check) {
   print "$_<br/>\n";
}
print end_html;
------------------------------------------------------------
Pragmas (perl 5.8.8) used :
  • strict - Perl pragma to restrict unsafe constructs
  • warnings - Perl pragma to control optional warnings
Core (perl 5.8.8) Modules used :
  • CGI - Simple Common Gateway Interface Class
  • CGI::Carp - CGI routines for writing to the HTTPD (or other) error log


this is just totally wrong:

CODE

if($cgi->param('$formdata['remember']') eq 'on'){

incorrect use of quotes ' and brackets [].

------------------------------------------
- Kevin, perl coder unexceptional! wiggle

chrismassey (Programmer)
22 Nov 07 12:04
Hey,

Yeah I have always connected to a HTML form using param('name');

I just realised that a checkbox is only given its value if its selected (so only those which are ticked will be passed into @check), so just as Kevin has written it.

CODE

#HTML
<input type="checkbox" name="check" value="1">
<input type="checkbox" name="check" value="2">
<input type="checkbox" name="check" value="3">
<input type="checkbox" name="check" value="4">
<input type="checkbox" name="check" value="5">

#PERL
my @checks = param('check');
foreach (@check) {
          print "Checkbox ON - VALUE = $_\n\n";
}

I know that if a checkbox is ticked and doesn't have a pre-set value then it is by default given the value of ON, however I have had probems in the past trying to check the default value, therefore I always give checkboxes individual values, preferably numerical.

Chris
chrismassey (Programmer)
22 Nov 07 12:10
Oh and don't forget...

Use == for numerical camparisons
Use eq for string comparisons

So it would be - if ($formdata{'usrname'} eq "on") {

Althought I suggest just using:
my @check = param('check');
foreach @check { #do something }

And just one more thing. You recieve a blank page most likely because nothing is passed into @check. As I said above, I often have the same problem unless I use numerical checkbox values.

Chris
chrismassey (Programmer)
22 Nov 07 12:12
:/

CODE

my @check = param('check');
foreach (@check) {
          print "Checkbox ON - VALUE = $_\n\n";
}

(I made the same syntax error as you whilst copying your code hehe. @checks should be @check)

Chris
chrismassey (Programmer)
22 Nov 07 12:35
Okay last response just because its annoying me hehe,

shao, your last posted code will not work because it seems all messed up:

Quote:


HTML
<input type="checkbox" name="remember" value="on">

PERL/CGI
my @checks = param($formdata{'usrname'});
foreach (@check) {
     if ($formdata{'usrname'} == "on") {
          print "Checkbox OFF\n\n";
     }
}

1) You have not connected to the checkbox name "remember", you have connected to the checkbox name "usrname"
2) You have called the declare array "checks" and the forach array "check". Both should be either checks or check
3) I believe that only checkbox values which have been ticked are passed into @check/@checks. And then you don't even use @check/@checks specifically. You simply check the same condition over and over for every checkbox that was ticked (i.e. if 3 checkboxes were ticked, the same condition would process 3 times).
4) Why are you printing "Checkbox OFF" when the if statement checks if the checkbox is ON?
5) Therefore if the syntax was correct then you wouldn't get the expected result anyway

Chris

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close