Deanznet Posted April 12, 2009 Share Posted April 12, 2009 I have a form that post to register.php Register.php if ($_POST['fname']=='' || strlen($_POST['fname'])<3) { $errors[] = 'First name is required and must contain 3 characters or more'; } if ($_POST['lname']=='' || strlen($_POST['lname'])<3) { $errors[] = 'Last name is required and must contain 3 characters or more'; } if ($_POST['spassword']=='' || alpha_numeric($_POST['spassword'])==FALSE) { $errors[] = 'A password is required and must be alpha-numeric'; } if ($_POST['spassword']!=$_POST['passwordagain']) { $errors[] = 'The two passwords must match'; } if (valid_email($_POST['semail'])==FALSE) { $errors[] = 'Please supply a valid email address'; } if(is_array($errors)) { echo '<p class="error"><b>The following errors occured</b></p>'; while (list($key,$value) = each($errors)) { echo '<span class="error"><ul><li>'.$value.'</span><br /></ul>'; } } else { echo '<p><b>Success!</b></p>'; echo '<span>Your registration was successfully processed. You may login and start using your account. Thank you for registering !</span>'; } <input id="terms-chk" name="terms" value="checkbox" type="checkbox" /> Im trying to make it if its not check to do an error.. It tired if($_POST['terms-chk'] == "off") { $errors[] = 'You must agree to the Terms of Use.'; } But that did not work. Quote Link to comment https://forums.phpfreaks.com/topic/153777-checking-if-a-checkbox-is-checked-help/ Share on other sites More sharing options...
jackpf Posted April 12, 2009 Share Posted April 12, 2009 You've set its value to checkbox, so you need to do if($_POST['terms-chk'] == 'checkbox') { echo 'you agreed to the terms'; } Quote Link to comment https://forums.phpfreaks.com/topic/153777-checking-if-a-checkbox-is-checked-help/#findComment-808192 Share on other sites More sharing options...
premiso Posted April 12, 2009 Share Posted April 12, 2009 if(!isset($_POST['terms-chk'])) Use isset. If a checked box is not checked is does not get passed to the page in the POST array. (At least I think that is how it works ) Quote Link to comment https://forums.phpfreaks.com/topic/153777-checking-if-a-checkbox-is-checked-help/#findComment-808193 Share on other sites More sharing options...
jackpf Posted April 12, 2009 Share Posted April 12, 2009 Forgive me if I am incorrect, but I don't think that would work, premiso. The terms-chk will be included in the post array whether it's checked or not. You'd only be able to check if it existed with your code. If I am correct, the only way to check if a checkbox is actually checked is to compare it with its value. Quote Link to comment https://forums.phpfreaks.com/topic/153777-checking-if-a-checkbox-is-checked-help/#findComment-808196 Share on other sites More sharing options...
premiso Posted April 12, 2009 Share Posted April 12, 2009 <?php if (isset($_POST['check1'])) { echo $_POST['check1'] . " check1 was checked <br />"; } if (isset($_POST['check2'])) { echo $_POST['check2'] . " check2 was checked <br />"; } echo <<<OUTPUT <form action="test.php" method="POST"> <input type="checkbox" name="check1" value="check1" /><br /> <input type="checkbox" name="check2" value="check2" /> <input type="submit" value="Submit" /> </form> OUTPUT; ?> Nope, isset works just fine as I suspected. Give it a shot. Quote Link to comment https://forums.phpfreaks.com/topic/153777-checking-if-a-checkbox-is-checked-help/#findComment-808199 Share on other sites More sharing options...
Kalland Posted April 12, 2009 Share Posted April 12, 2009 Hi, Isset will work. But its the name attribute of the input tag that you must use, not the id attribute when you check whether its checked or not. Quote Link to comment https://forums.phpfreaks.com/topic/153777-checking-if-a-checkbox-is-checked-help/#findComment-808205 Share on other sites More sharing options...
laffin Posted April 12, 2009 Share Posted April 12, 2009 yes, checkboxes dun return a $_POST variable if not checked so best way is the isset method. Quote Link to comment https://forums.phpfreaks.com/topic/153777-checking-if-a-checkbox-is-checked-help/#findComment-808210 Share on other sites More sharing options...
Deanznet Posted April 12, 2009 Author Share Posted April 12, 2009 Yes Thank you! It worked! Quote Link to comment https://forums.phpfreaks.com/topic/153777-checking-if-a-checkbox-is-checked-help/#findComment-808212 Share on other sites More sharing options...
jackpf Posted April 12, 2009 Share Posted April 12, 2009 I stand corrected. Quote Link to comment https://forums.phpfreaks.com/topic/153777-checking-if-a-checkbox-is-checked-help/#findComment-808241 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.