dwest Posted January 25, 2007 Share Posted January 25, 2007 This is rudimentary I know...I have a checkbox with name="foo"On posting the form, what is the syntax for testing if foo is checked?If ($_POST['foo']...what goes here?) { do stuff }Thanks much! Link to comment https://forums.phpfreaks.com/topic/35688-checkbox-question-testing-for-checked/ Share on other sites More sharing options...
kenrbnsn Posted January 25, 2007 Share Posted January 25, 2007 The values for checkbox are only returned when the checkbox is checked, you can do something like this:[code]<?phpif (isset($_POST['foo'])){//// do processing//}?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/35688-checkbox-question-testing-for-checked/#findComment-169075 Share on other sites More sharing options...
dwest Posted January 25, 2007 Author Share Posted January 25, 2007 jeez! no wonder I couldn't figure it out.Does this return checked or not checked or what??? :DThanks! Link to comment https://forums.phpfreaks.com/topic/35688-checkbox-question-testing-for-checked/#findComment-169082 Share on other sites More sharing options...
boo_lolly Posted January 25, 2007 Share Posted January 25, 2007 this is untested, but you could also do this:[code]<?php if(isset($_POST['checkboxarray'])){ $true = 0; $false = 0; foreach($_POST['checkboxarray'] as $key => $val){ if($val == true){ $true++; }else{ $false++; } } echo $true .' checkboxes were checked<br />\n'; echo $false .' checkboxes were NOT checked<br />\n'; } echo '<form method="post" action="'. $_SERVER['php_self'] .'">'; for($i = 0; $i <= $num_of_checkboxes; $i++){ echo '<input type="checkbox" name="'. $checkboxarray[$i] .'">'; } echo '</form>';?>[/code] Link to comment https://forums.phpfreaks.com/topic/35688-checkbox-question-testing-for-checked/#findComment-169084 Share on other sites More sharing options...
dwest Posted January 25, 2007 Author Share Posted January 25, 2007 Thanks much! Link to comment https://forums.phpfreaks.com/topic/35688-checkbox-question-testing-for-checked/#findComment-169087 Share on other sites More sharing options...
kenrbnsn Posted January 25, 2007 Share Posted January 25, 2007 The value is only set when the checkbox is checked. If is is not checked, the isset() will return "false". If it is set, the value of the checkbox will be in $_POST['foo']Ken Link to comment https://forums.phpfreaks.com/topic/35688-checkbox-question-testing-for-checked/#findComment-169088 Share on other sites More sharing options...
boo_lolly Posted January 25, 2007 Share Posted January 25, 2007 i just modified my code, take a look at it again. it's a little more efficient i think. Link to comment https://forums.phpfreaks.com/topic/35688-checkbox-question-testing-for-checked/#findComment-169091 Share on other sites More sharing options...
dwest Posted January 25, 2007 Author Share Posted January 25, 2007 Many thanks to both of you for clarifying this for me! It's amazing how dang simple the solutions to some of my stumbles are. Makes one feel pretty dumb sometimes ::)Live and learn... Link to comment https://forums.phpfreaks.com/topic/35688-checkbox-question-testing-for-checked/#findComment-169094 Share on other sites More sharing options...
boo_lolly Posted January 25, 2007 Share Posted January 25, 2007 [quote author=dwest link=topic=124010.msg513320#msg513320 date=1169746965]Many thanks to both of you for clarifying this for me! It's amazing how dang simple the solutions to some of my stumbles are. Makes one feel pretty dumb sometimes ::)Live and learn...[/quote]don't sweat it man. we've all asked our fair share of questions around here.just remember: there are no dumb questions. just dumb people =P. Link to comment https://forums.phpfreaks.com/topic/35688-checkbox-question-testing-for-checked/#findComment-169106 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.