Help! I’m new to php so stick with me! I have a page that loops through my database and prints student names with a checkbox alongside each name. When you select a checkbox and hit submit, it saves a value of 1 to the db.
This is working fine but what i’m trying to so is that if the checkbox is already populated and the user unchecks it, i want it ti save the db back to 0.
I know you can’t pass values of an unchecked checkbox. So when the user submits the form, i’m trying to write everything in the db to 0, before it starts looping through the checkboxes and setting the appropiate values to 1, where they are checked. That way whatever has been unchecked will go back to 0.
Here is some of the php code in the form displaying the checkboxes. The LogID is the PK in the table where the column name ‘Value’ is either 0 or 1.
There is a submit button at the end of this form. Here’s the code for when this button is clicked.
<?php
include (“connect.php”);
if ($_POST[‘Save’])
{
if(isset($_POST[‘box’])){
$box = $_POST['box'];
while (list ($key,$val) = @each ($box)) {
$sqlupdate="UPDATE tblReportLog SET `Value` = '1' WHERE LogID='$val'";
$resdel=mysql_query($sqlupdate);
}
}
else{
echo “No records selected.”;
}
}
?>
Is it possible to put a query somewhere in here wher i can the checkbox value to 0?
Or would an alternative be to put a ‘hidden’ value in the first page and loop through the values that are recorded by this for every entry in the table with LogID as the PK.
Sorry if this is confusing but any help on this would be great!!
Thanks for the reply. So do i put something like this before the while loop?
$sqlupdate2=“UPDATE tblReportLog SET Value = ‘0’ WHERE LogID=‘$val’”;
$resdel=mysql_query($sqlupdate2);
$box = $_POST[‘box’];
while (list ($key,$val) = @each ($box)) {
$sqlupdate=“UPDATE tblReportLog SET Value = ‘1’ WHERE LogID=‘$val’”;
$resdel=mysql_query($sqlupdate);
}
How can i get the value in ‘$val’ before the loop starts? Will i need another loop to loop through all the entries in the db and set them to 0 where the LogID =$val?
Here’s what I use. The advantage to this is you don’t have to update every row in the table. The disadvantage is it requires javascript.
//////////////////////////////////
// CHECKBOX
// Problem: An un-checked checkbox in a form does not send anything back to the server.
// So, if a box filled from a database field was checked, the user un-checks it and posts,
// nothing changes because un-checked checkboxes do not send(post) any value.
//
// This function creates html with a checkbox and a hidden
// field such that the value is posted whether or not the box is checked.
// the checkbox name is NOT the field name, rather the hidden field is the "live" posting value.
// javascript sets it when the box is unchecked
//
//Sample calls:
// echo checkBox('on','mytest','on','off');
// $html .= '<tr><th>Item Active</th><td>' . checkBox($row['item_active'],'item_active') . '<td><tr>' . "\
";
//////////////////////////////////
function checkBox($current_value,$name,$checked_value=1,$unchecked_value=0) {
if($current_value == $checked_value) {
$checked = 'checked="checked"'; // confused yet?
} else {
$current_value = $unchecked_value;
}
$html .= '<input type="checkbox" name="' . $name . '_cb" ' . $checked . ' onclick="set_hidden_from_checkbox(this,\\'' . $name . '_ID\\',\\'' . $checked_value . '\\',\\'' . $unchecked_value . '\\');" />' . "\
";
$html .= '<input type="hidden" name="' . $name . '" id="' . $name . '_ID" value="' . $current_value . '"/>' . "\
";
return $html;
}