scarlson Posted February 2, 2008 Share Posted February 2, 2008 I am trying to store the value of my checkbox in my mySQL database. I have tried multiple things but nothing has worked yet. In the database I have it setup to store a bool, is that what i want to do. I imagined that checked would equal 1 and unchecked would be 0. I am wanting to store the value so i can use it later in a query to see if this item was checked or not. Any help or suggestions would be great. Scott Quote Link to comment https://forums.phpfreaks.com/topic/89011-solved-working-with-checkboxes/ Share on other sites More sharing options...
ryeman98 Posted February 2, 2008 Share Posted February 2, 2008 I'm not sure I fully understand ... do you want to post the code? Quote Link to comment https://forums.phpfreaks.com/topic/89011-solved-working-with-checkboxes/#findComment-455835 Share on other sites More sharing options...
laffin Posted February 2, 2008 Share Posted February 2, 2008 nope. checkboxes return nothing, if unchecked or something if checked (as a value can be attached to the tag) so $mycheckbox=isset($_POST['mycheckbox'])?1:0; Quote Link to comment https://forums.phpfreaks.com/topic/89011-solved-working-with-checkboxes/#findComment-455836 Share on other sites More sharing options...
ryeman98 Posted February 2, 2008 Share Posted February 2, 2008 In your tag, just put value="1" in the checkbox tag. If it isn't checked, the value will be 0. Quote Link to comment https://forums.phpfreaks.com/topic/89011-solved-working-with-checkboxes/#findComment-455839 Share on other sites More sharing options...
scarlson Posted February 2, 2008 Author Share Posted February 2, 2008 Ok, still not working. Here is the code I am trying to do now: <INPUT TYPE=CHECKBOX NAME="gSale" value="0">Place a check if ad is for a Garage Sale<P> This is the setup for the checkbox $gSale=isset($_POST['gSale'])?1:0; This is the code I am using to get the value if it's checked or not, then using $gSale to store that in the database. It always shows as 0 Quote Link to comment https://forums.phpfreaks.com/topic/89011-solved-working-with-checkboxes/#findComment-455885 Share on other sites More sharing options...
ryeman98 Posted February 2, 2008 Share Posted February 2, 2008 Ok, still not working. Here is the code I am trying to do now: <INPUT TYPE=CHECKBOX NAME="gSale" value="0">Place a check if ad is for a Garage Sale<P> This is the setup for the checkbox $gSale=isset($_POST['gSale'])?1:0; This is the code I am using to get the value if it's checked or not, then using $gSale to store that in the database. It always shows as 0 No, the value must be 1. I'd say just starting out simple: <input type="checkbox" name="gSale" value="1"> <?php $gSale = $_POST['gSale']; if ($gSale == 1) { // Was checked } ?> Quote Link to comment https://forums.phpfreaks.com/topic/89011-solved-working-with-checkboxes/#findComment-455887 Share on other sites More sharing options...
scarlson Posted February 2, 2008 Author Share Posted February 2, 2008 Ok, still not working. Here is the code I am trying to do now: <INPUT TYPE=CHECKBOX NAME="gSale" value="0">Place a check if ad is for a Garage Sale<P> This is the setup for the checkbox $gSale=isset($_POST['gSale'])?1:0; This is the code I am using to get the value if it's checked or not, then using $gSale to store that in the database. It always shows as 0 No, the value must be 1. I'd say just starting out simple: <input type="checkbox" name="gSale" value="1"> <?php $gSale = $_POST['gSale']; if ($gSale == 1) { // Was checked } ?> So I tried setting the value to 1 but I am still not getting a 1 if it's checked. So by setting the value to 1 in the checkbox tag if it's checked it will hold a value of 1? I'm not getting it. Quote Link to comment https://forums.phpfreaks.com/topic/89011-solved-working-with-checkboxes/#findComment-455890 Share on other sites More sharing options...
laffin Posted February 2, 2008 Share Posted February 2, 2008 not working? <?php if($_SERVER['REQUEST_METHOD']=='POST') echo "<PRE>". print_r($_POST,true) ."</PRE>"; ?> <html> <body> <form method="POST"> <INPUT TYPE='checkbox' name='cb[]' value='0'> <INPUT TYPE='checkbox' name='cb[]' value='1'> <INPUT TYPE='checkbox' name='cb[]' value='2'> <INPUT TYPE='checkbox' name='cb[]' value='3'> <INPUT TYPE='checkbox' name='cb[]' value='4'> <INPUT type="submit"> </FORM> </body> </html> as i said, unchecked checkboxed return nothing, while checked boxes return the value. Quote Link to comment https://forums.phpfreaks.com/topic/89011-solved-working-with-checkboxes/#findComment-455902 Share on other sites More sharing options...
budimir Posted February 2, 2008 Share Posted February 2, 2008 $name_variable = (isset($_POST['checkbox_name']) ? 1 : 0); Try using VARCHAR in SQL not BOOL, it's much simplier. Maybe bool is causing all of your problems! Quote Link to comment https://forums.phpfreaks.com/topic/89011-solved-working-with-checkboxes/#findComment-456048 Share on other sites More sharing options...
laffin Posted February 2, 2008 Share Posted February 2, 2008 by any chance are ya encasing the 1 or 0 in quotes, like a literal string? if so that wud be a problem Quote Link to comment https://forums.phpfreaks.com/topic/89011-solved-working-with-checkboxes/#findComment-456066 Share on other sites More sharing options...
scarlson Posted February 2, 2008 Author Share Posted February 2, 2008 $name_variable = (isset($_POST['checkbox_name']) ? 1 : 0); Try using VARCHAR in SQL not BOOL, it's much simplier. Maybe bool is causing all of your problems! Thanks, that worked by changing it to a VARCHAR Quote Link to comment https://forums.phpfreaks.com/topic/89011-solved-working-with-checkboxes/#findComment-456073 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.