webref.eu Posted July 22, 2008 Share Posted July 22, 2008 Hi All I am creating a registration form that has a "Subscribe to Newsletter" checkbox on it. If ticked, I want to record a value of 1 in my database, otherwise a value of 0. This is the html in my form: <input type="checkbox" name="SubscribeToNewsletter" /> Please could someone give me the code to get the desired values into the database. Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/116066-simple-checkbox-to-database-question/ Share on other sites More sharing options...
dezkit Posted July 22, 2008 Share Posted July 22, 2008 <input type="checkbox" name="SubscribeToNewsletter" value="1"/> <?php $SubscribeToNewsletter = $_POST['SubscribeToNewsletter']; if($SubscribeToNewsletter = "1"){ // mysql shit up in this bitchy, ya digg? } Quote Link to comment https://forums.phpfreaks.com/topic/116066-simple-checkbox-to-database-question/#findComment-596809 Share on other sites More sharing options...
craygo Posted July 22, 2008 Share Posted July 22, 2008 Thing with checkboxes is they are not passed if they are not checked. so you will have to put a check before you insert into the database $SubscribeToNewsletter = isset($_POST['SubscribeToNewsletter']) ? "1" : "0"; Now if it is set $subscribeToNewletter will equal 1 if not is will be equal to 0; Ray Quote Link to comment https://forums.phpfreaks.com/topic/116066-simple-checkbox-to-database-question/#findComment-596813 Share on other sites More sharing options...
revraz Posted July 22, 2008 Share Posted July 22, 2008 When you use IF, you need two "==", not one. <input type="checkbox" name="SubscribeToNewsletter" value="1"/> <?php $SubscribeToNewsletter = $_POST['SubscribeToNewsletter']; if($SubscribeToNewsletter = "1"){ // mysql shit up in this bitchy, ya digg? } Quote Link to comment https://forums.phpfreaks.com/topic/116066-simple-checkbox-to-database-question/#findComment-596815 Share on other sites More sharing options...
.josh Posted July 22, 2008 Share Posted July 22, 2008 Thing with checkboxes is they are not passed if they are not checked. so you will have to put a check before you insert into the database $SubscribeToNewsletter = isset($_POST['SubscribeToNewsletter']) ? "1" : "0"; Now if it is set $subscribeToNewletter will equal 1 if not is will be equal to 0; Ray well technically that doesn't matter. if it's set it will equal 1. If it's not set, it won't be equal 1. Therefore doing <?php $SubscribeToNewsletter = $_POST['SubscribeToNewsletter']; if($SubscribeToNewsletter = "1"){ // mysql shit up in this bitchy, ya digg? } will work. Well, aside from him needing == not =. Also, since the posted value already equals 1, why make a new variable assigning 1 or 0? Simply use the original var. On the other hand, the posted var would need to be sanitized and your ternary inadvertently does that by assigning it 1 no matter what value was posted. Quote Link to comment https://forums.phpfreaks.com/topic/116066-simple-checkbox-to-database-question/#findComment-596820 Share on other sites More sharing options...
webref.eu Posted July 22, 2008 Author Share Posted July 22, 2008 Thanks for your help everybody, just using: <input type="checkbox" name="SubscribeToNewsletter" value="1" /> and later ... $SubscribeToNewsletter = $_POST['SubscribeToNewsletter']; is giving me the 0 value when not checked and the 1 when checked as I wanted. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/116066-simple-checkbox-to-database-question/#findComment-596866 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.