Peggy Posted August 28, 2009 Share Posted August 28, 2009 I have a form with check boxes in it. I didn't set the value of the check boxes I always thought the value was either 'on' or '' (empty). I have the value 'on' passing to the database. The problem is when I unchecked the box it isn't changing the value in the database to '' (empty). The rest of the values of the form, such as input boxes are being changed. Here is my code on my check boxes <input type="checkbox" name="form3_one_2" <?php if($form3_one_2 == 'on'){print 'checked="checked"';}?> /> Am I supposed to have an value put into the form??? Link to comment https://forums.phpfreaks.com/topic/172342-changing-the-value-of-a-checkbox-in-the-database/ Share on other sites More sharing options...
asmith Posted August 29, 2009 Share Posted August 29, 2009 there's no need to touch your checkbox in the form. If the value '' (empty) is not being recorded in your database, Then your php code is not sending it. Can we see your code the part you are handling the $_POST data and recording it to database table? Link to comment https://forums.phpfreaks.com/topic/172342-changing-the-value-of-a-checkbox-in-the-database/#findComment-908771 Share on other sites More sharing options...
Peggy Posted August 29, 2009 Author Share Posted August 29, 2009 I don't think there is anything wrong with the code I'm using to post the info to the data base because the input boxes are being updated. Here is the code that updates the form anyway. if(!empty($form_id)){ $sql = "UPDATE ".$form." SET "; foreach($_POST as $key => $value){ if(($key != 'submit_'.$form ) && ($key != 'password2') && ($key != 'applicant_id') ){ $sql .= $key. " = '$value',"; } } $sql = substr($sql, 0, -1); $sql .= " WHERE ".$applicant_id." = $applicant_id"; $result = mysql_query($sql,$db) or die(mysql_error(). "<br />SQL: $sql"); } Link to comment https://forums.phpfreaks.com/topic/172342-changing-the-value-of-a-checkbox-in-the-database/#findComment-908808 Share on other sites More sharing options...
Peggy Posted August 29, 2009 Author Share Posted August 29, 2009 The first time that the form is submitted the value is being passed. It is just that after the value is in the data base I cant erase it. Link to comment https://forums.phpfreaks.com/topic/172342-changing-the-value-of-a-checkbox-in-the-database/#findComment-908811 Share on other sites More sharing options...
asmith Posted August 30, 2009 Share Posted August 30, 2009 I don't recommend this way of coding. But you can modify it so that it gives you what you need: I call the checkbox field "chkField" <?php if(!empty($form_id)){ $sql = "UPDATE ".$form." SET "; foreach($_POST as $key => $value){ if(($key != 'submit_'.$form ) && ($key != 'password2') && ($key != 'applicant_id') ){ $sql .= $key. " = '$value',"; } } if (!isset($_POST['chkField']) || $_POST['chkField'] == '') $sql .= "chkField = '',"; $sql = substr($sql, 0, -1); $sql .= " WHERE ".$applicant_id." = $applicant_id"; $result = mysql_query($sql,$db) or die(mysql_error(). "<br />SQL: $sql"); } ?> Link to comment https://forums.phpfreaks.com/topic/172342-changing-the-value-of-a-checkbox-in-the-database/#findComment-909155 Share on other sites More sharing options...
redarrow Posted August 30, 2009 Share Posted August 30, 2009 re think the whole idea i guess. ever used the database default setting looks like a whole wast of code. Link to comment https://forums.phpfreaks.com/topic/172342-changing-the-value-of-a-checkbox-in-the-database/#findComment-909164 Share on other sites More sharing options...
Peggy Posted August 30, 2009 Author Share Posted August 30, 2009 the code: if (!isset($_POST['chkField']) || $_POST['chkField'] == '') $sql .= "chkField= '',"; works great only I have 200 checkboxes how can I incorporate this code into the loop: foreach($_POST as $key => $value){} I have also found this code. $one = isset($_POST['chkField']) ? 'on' : 'off'; this code works also but I haven't figured out how to put it into the loop foreach($_POST as $key => $value){} Link to comment https://forums.phpfreaks.com/topic/172342-changing-the-value-of-a-checkbox-in-the-database/#findComment-909402 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.