jaArch Posted November 7, 2011 Share Posted November 7, 2011 Hi: How do I check to see if a checkbox has not been checked? Would the same rules apply to a radiobutton? This is what I have: <input name="status[1]" id="approved" value="<?php echo $_POST['approved'] ?>" type="checkbox" /> <label for="approved">Approved</label> if (isset($_POST['status[1]'])): $errMsg['status[1]'] = "Must select a status."; endif; I tested it out, but it just prints the error message even if I have checked it Quote Link to comment https://forums.phpfreaks.com/topic/250597-checking-if-checkbox-is-not-checked/ Share on other sites More sharing options...
watsmyname Posted November 7, 2011 Share Posted November 7, 2011 The checkbox has value only if its checked. you can try this <?php if ($_POST['status'][1]==""): $errMsg['status[1]'] = "Must select a status."; endif; ?> Quote Link to comment https://forums.phpfreaks.com/topic/250597-checking-if-checkbox-is-not-checked/#findComment-1285759 Share on other sites More sharing options...
jaArch Posted November 7, 2011 Author Share Posted November 7, 2011 That one didn't really work. So I tried this: foreach($_POST['location'] as $value){ if (isset($_POST['value'])): $succMsg['location'] = "LOCATION = {$_POST['location']}"; else: $errMsg['location'] = "Must select a location."; endif; } <input name="location[]" id="radio1" value="<?=$_POST['garage'] ?>" type="radio" /> <label for="radio1">Garage</label> <input name="location[]" id="radio2" value="<?=$_POST['home'] ?>" type="radio" /> <label for="radio1">Home</label> But when I test it out it only prints "Array" instead of what I originally selected. Could you help me with this part as well? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/250597-checking-if-checkbox-is-not-checked/#findComment-1285870 Share on other sites More sharing options...
ManiacDan Posted November 7, 2011 Share Posted November 7, 2011 foreach($_POST['location'] as $value){ That implies that your $_POST['location'] is an array, but later you try to use it as a string. Perhaps you meant to use $value directly? Quote Link to comment https://forums.phpfreaks.com/topic/250597-checking-if-checkbox-is-not-checked/#findComment-1285885 Share on other sites More sharing options...
jaArch Posted November 7, 2011 Author Share Posted November 7, 2011 foreach($_POST['location'] as $value){ That implies that your $_POST['location'] is an array, but later you try to use it as a string. Perhaps you meant to use $value directly? Yeah, I've tried that: foreach($_POST['location'] as $value){ if (isset($_POST['value'])): $succMsg['location'] = "LOCATION = $value"; else: $errMsg['location'] = "Must select a location."; endif; } It just says LOCATION = when I select something Quote Link to comment https://forums.phpfreaks.com/topic/250597-checking-if-checkbox-is-not-checked/#findComment-1286020 Share on other sites More sharing options...
ManiacDan Posted November 7, 2011 Share Posted November 7, 2011 do this: echo "<pre>" . print_r($_POST,1) . "</pre>"; That will show you what the actual contents of $_POST are, including any nested arrays. Work from there. We can't keep guessing at what the proper structure of your code is without knowing what's coming into post. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/250597-checking-if-checkbox-is-not-checked/#findComment-1286034 Share on other sites More sharing options...
jaArch Posted November 7, 2011 Author Share Posted November 7, 2011 Well I tried to switch it up a bit and am officially stumped...here's my entire code: <? $PROVINCES = array( "--" => "--- Please Select Provinces ---", "nf"=>"Newfoundland", "pe"=>"Prince Edward Island", "nb"=>"New Brunswick", "ns"=>"Nova Scotia", "qc"=>"Quebec", "on"=>"Ontario", "mb"=>"Manitoba", "sk"=>"Saskatchewan", "ab"=>"Alberta", "bc"=>"British Columbia", "nt"=>"Northwest Territories"); if ($_SERVER['REQUEST_METHOD'] == 'POST'): if (empty($_POST['name'])): $errMsg['name'] = "Name cannot be empty."; else: $succMsg['name'] = "NAME = {$_POST['name']}"; endif; if (empty($_POST['city'])): $errMsg['city'] = "City must not be empty."; else: $succMsg['city'] = "CITY = {$_POST['city']}"; endif; if (isset($_POST['status1'])): $succMsg['status1'] = "STATUS = {$_POST['status1']}"; else: $errMsg['status1'] = 'Must select a status.'; endif; foreach($_POST['location'] as $value){ if (isset($_POST['value'])): $succMsg['location'] = "LOCATION = ".$_POST['value']; else: $errMsg['location'] = "Must select a location."; endif; } if (isset($_POST['province'])): $succMsg['province'] = "PROVINCE = {$_POST['province']}"; else: $errMsg['province'] = "Must select a province."; endif; if(ereg('(Mr\.|Mrs\.)',$_POST['name']) == false): $errMsg['fullname'] = "Fullname not entered correctly."; endif; if (count($errMsg) > 0): $dispErrMsgs = true; else: $dispSuccessMsg = true; endif; endif; ?> <html> <head> </head> <body> <p><a href="<?= $_SERVER['PHP_SELF'] ?> ">Refresh This Page</a></p> <? if ($dispErrMsgs): ?> <p>There are errors in the code: </p> <ol> <? foreach($errMsg as $errMsgs ): ?> <li><?= $errMsgs ?></li> <? endforeach; ?> </ol> <? endif; ?> <? if ($dispSuccessMsg): ?> <p>Thank you for your submission. </p> <ol> <? foreach($succMsg as $succMsgs ): ?> <li><?= $succMsgs ?></li> <? endforeach; ?> <? endif; ?> <form action="<?= $_SERVER['PHP_SELF'] ?>" name="getstuff" method="post" > <input type="hidden" name="_act" value="post"> <label>Name</label> <p> <input name="name" type="text" class="textbox" value="<? $_POST['name']; ?>" size="20"> </p> <label>City</label> <p> <input name="city" type="text" class="textbox" value="<? $_POST['city']; ?>" size="20"> </p> <label>Location</label> <p> <input name="location1" id="radio1" value="<?=$_POST['garage'] ?>" type="radio" /> <label for="radio1">Garage</label> <input name="location2" id="radio2" value="<?=$_POST['home'] ?>" type="radio" /> <label for="radio1">Home</label> </p> <label>Provinces</label> <p> <select name = "province[]" multiple size = "12" id="type" > <? foreach ($PROVINCES as $abbreviation => $longname){ ?> <option value = "<?= $abbreviation ?>"> <?= $longname ?></option> <? } ?> </select> </p> <label>Select status</label> <p> <input name="status1" id="approved" value="<?= $_POST['approved'] ?>" type="checkbox" /> <label for="approved">Approved</label> <input name="status2" id="pending" value="<?=$_POST['pending'] ?>" type="checkbox" /> <label for="pending">Pending Application</label> <input name="status[]" id="actives" value="actives" type="checkbox" /> <label for="actives">Active Service</label> </p> <p> <input name="_submit" type="submit" class="button" value="Submit"> </p> </form> <? $a = ('name'); $b = ('city'); $trim = ($a); $trim = ($b); echo "<pre>" . print_r($_POST,1) . "</pre>"; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/250597-checking-if-checkbox-is-not-checked/#findComment-1286061 Share on other sites More sharing options...
ManiacDan Posted November 8, 2011 Share Posted November 8, 2011 Congratulations, you failed to follow very basic instructions on how to debug. See where I said we won't keep guessing? Guess what happens now. Quote Link to comment https://forums.phpfreaks.com/topic/250597-checking-if-checkbox-is-not-checked/#findComment-1286187 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.