simflex Posted July 17, 2010 Share Posted July 17, 2010 Greetings again, good helpers. I have been trying for over 3 hours trying to figure out why the code I am using to insert checkbox value into the db isn't working. It doesn't matter whether I check the box or not, the value is always 0. Can you please assist again? Many thanks as always. //This works with radio button <input type=radio value=more name=repeat> //But the codes below don't work //var assignment $mainpage = (isset($_POST['mainpage']) && $_POST['mainpage'] == '1')? 1 : 0; //insert statement $query = "insert into ".$EVENTS_TB." values (null,'$timestamp','$title','$description','$url','$email','$cat','$stime','$etime','$bday','$bmonth','$byear','$approve','0','$nobody','','$loc','$mainpage')"; $result = mysql_query($query); //<form field echo translate("Main Page")."\n"; echo "<input type=checkbox name=mainpage value=1><br/>\n"; Link to comment https://forums.phpfreaks.com/topic/207997-inserting-value-from-checkbox-into-the-db-not-giving-required-value/ Share on other sites More sharing options...
kenrbnsn Posted July 17, 2010 Share Posted July 17, 2010 This test case works fine: <?php if (isset($_POST['submit'])) { echo '<pre>' . print_r($_POST,true) . '</pre>'; $mainpage = (isset($_POST['mainpage']) && $_POST['mainpage'] == '1')? 1 : 0; echo $mainpage . "<br>\n"; } ?> <html> <head> <title>test checkbox</title> </head> <body> <form action="" method="post"> Main Page: <input type="checkbox" name="mainpage" value="1" /> <input type="submit" value="Test" name="submit"> </form> </body> </html> I notice that you don't check to make sure your query is really working, so change <?php $result = mysql_query($query); ?> to <?php $result = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error()); ?> to see if there's a problem there. Ken Link to comment https://forums.phpfreaks.com/topic/207997-inserting-value-from-checkbox-into-the-db-not-giving-required-value/#findComment-1087327 Share on other sites More sharing options...
simflex Posted July 17, 2010 Author Share Posted July 17, 2010 Thanks for the response. Funny after trying the code you suggested, I don't get any warnings or errors and 0 is being getting inserted into the db. Just for starters, the datatype I am using for the mainpage filedname is tinyint(1). Link to comment https://forums.phpfreaks.com/topic/207997-inserting-value-from-checkbox-into-the-db-not-giving-required-value/#findComment-1087333 Share on other sites More sharing options...
kenrbnsn Posted July 17, 2010 Share Posted July 17, 2010 At the top of the processing script, put <?php echo '<pre>' . print_r($_POST,true) . '</pre>'; ?> this will dump the data that is being given to your script from your form. Make sure the value is being sent. Ken Link to comment https://forums.phpfreaks.com/topic/207997-inserting-value-from-checkbox-into-the-db-not-giving-required-value/#findComment-1087334 Share on other sites More sharing options...
simflex Posted July 17, 2010 Author Share Posted July 17, 2010 I get the correct values, when I pass a value of 1 to mainpage checkbox. If however, I don't check the box, it doesn't even show up on screen. Please see the 2 lists [title] => Request For Assistance [description] => Testing => [loc] => Training Room => [cat] => HS [mainpage] => 1 [starttimehr] => 12 [starttimemin] => 00 [startperiod] => [endtimehr] => 20 [endtimemin] => 00 [endperiod] => [bday] => 21 [bmonth] => 7 [byear] => 2010 [repeat] => one [rtimes] => 1 [rday] => 0 ----------------------------- [title] => New Test [description] => Testing again => [loc] => Helen => [cat] => FIRE [starttimehr] => 08 [starttimemin] => 00 [startperiod] => [endtimehr] => 09 [endtimemin] => 00 [endperiod] => [bday] => 15 [bmonth] => 7 [byear] => 2010 [repeat] => one [rtimes] => 1 [rday] => 0 Link to comment https://forums.phpfreaks.com/topic/207997-inserting-value-from-checkbox-into-the-db-not-giving-required-value/#findComment-1087337 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.