scampbell70 Posted February 18, 2015 Report Posted February 18, 2015 I have received so much help from this site and the amazing people on it. I am humbled by the amount of knowledge everyone here has. I am hoping that you can help me with something that I am struggling to figure out. I have a check box that successfully writes to my db but I can only get it to work with 1 and 0. I would like checked = yes unchecked to = no but I cant figure it out. Here is the code I am currently using. <strong>Active: *</strong> <input type="checkbox" name="Active" value="1" <?php if($Active == 1) echo 'checked="checked"'; ?>"/><br/> and $Active = (int)$_POST['Active']; Quote
falkencreative Posted February 18, 2015 Report Posted February 18, 2015 See http://www.html-form-guide.com/php-form/php-form-checkbox.html. If you want to use "yes" and "no" instead, you'll need to use "yes" for the value of the checkbox, check that $Active is "yes" rather than 1, and not convert the post data to an int (by removing the "(int)" in your code). Quote
scampbell70 Posted February 18, 2015 Author Report Posted February 18, 2015 I actually tried that example before I posted. I'm sure I'm doing something wrong and probably very simple. I learn best by doing things relevant to something I enjoy but I've only been at this web stuff a few weeks Quote
falkencreative Posted February 18, 2015 Report Posted February 18, 2015 Well, the code you posted above only works for using integers, not "yes" or "no". What does your code that doesn't work look like? What doesn't work about it? What error(s) are you getting? Quote
scampbell70 Posted February 18, 2015 Author Report Posted February 18, 2015 (edited) I got it working without any errors on my xampp setup by changing $Active = (int)$_POST['Active']; to $Active = !empty($_POST['Active']) ? 'yes' : 'no'; Edited February 19, 2015 by scampbell70 Quote
scampbell70 Posted February 19, 2015 Author Report Posted February 19, 2015 The check box doesn't stay checked. Is there a way to fix that? Quote
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.
Note: Your post will require moderator approval before it will be visible.