Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check if checkbox is checked

Status
Not open for further replies.

shaolinf

Programmer
Nov 20, 2007
14
GB
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'){
 
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";
     }
}

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
 
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";
}
}
 
#HTML
<input type="checkbox" name="check" value="foo">
<input type="checkbox" name="check" value="bar">

#PERL
Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]
[black][b]use[/b][/black] [green]CGI::Carp[/green] [red]qw/[/red][purple]fatalsToBrowser[/purple][red]/[/red][red];[/red]
[black][b]use[/b][/black] [green]CGI[/green] [red]qw/[/red][purple]:standard[/purple][red]/[/red][red];[/red]
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] header, start_html[red];[/red]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@checks[/blue] = [maroon]param[/maroon][red]([/red][red]'[/red][purple]check[/purple][red]'[/red][red])[/red][red];[/red]
[olive][b]foreach[/b][/olive] [red]([/red][blue]@check[/blue][red])[/red] [red]{[/red]
   [black][b]print[/b][/black] [red]"[/red][purple][blue]$_[/blue]<br/>[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]
[black][b]print[/b][/black] end_html[red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]CGI - Simple Common Gateway Interface Class[/li]
[li]CGI::Carp - CGI routines for writing to the HTTPD (or other) error log[/li]
[/ul]
[/tt]

this is just totally wrong:

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

incorrect use of quotes ' and brackets [].



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
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
 
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
 
:/

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
 
Okay last response just because its annoying me hehe,

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

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top