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'];
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).
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
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?
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
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?
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now