dimanche 29 avril 2018

checkbox could not uncheck php

I have written a code to save the check values of checkboxes to database.

check condition is working fine as in the checked values are saving to database when submit and it keeps checked when submit and refresh.

But when it uncheck and submit then refresh the page the checkbox still keeps remained checked. It won't saved the status of unchecked.

I include a chunk code which is related to this issue. If anyone can get the issue that'd be great.

   $sql_stamp = "SELECT check_list FROM time_stamps WHERE nJobNumber=".$nJobNumber;
                $query_stamp = $conn->prepare($sql_stamp);
                $query_stamp->execute();
                $numRows = $query_stamp->rowCount(); 
                if($numRows > 0){
                    $row_stamp = $query_stamp->fetch(PDO::FETCH_ASSOC);
                    $checkboxes = explode(',',$row_stamp['check_list']);
                    echo ' <td><center> <input type="checkbox" name="check_list[]" value="shop_drawing" '.(in_array( "shop_drawing",$checkboxes) ? "checked=checked" : "").'></td>';
                    echo ' <td><center><input type="checkbox" name="check_list[]" value="site_measure" '.(in_array( "site_measure",$checkboxes) ? "checked=checked" : "").'></td>';
                    echo ' <td><center><input type="checkbox" name="check_list[]" value="shop_drawings_approved" '.(in_array( "shop_drawings_approved",$checkboxes) ? "checked =checked" : "").'></td>';
                    echo '<td><center> <input type="checkbox" name="check_list[]" id="check" value="batch" '.(in_array( "batch",$checkboxes) ? "checked=checked" : "").'></td>';
                    echo '<td><center> <input type="text" name="batch_no"><br>  </td>';
                    echo ' <td><center> <input type="submit" name="submit" value="Submit"/> </td>';
                    echo '<td><input type="hidden" name="job_number[result]" value="'."true".'"</td>'; 
                    echo '<td><input type="hidden" name="job_number[number]" value="'.$nJobNumber.'"</td>';
                }else{
                    echo '<td><center><input type="checkbox" name="check_list[]" id="check" value="shop_drawing"></td>';
                    echo '<td><center><input type="checkbox" name="check_list[]" id="check" value="site_measure"></td>';
                    echo '<td><center><input type="checkbox" name="check_list[]" id="check" value="shop_drawings_approved"> </td>';
                    echo '<td><center> <input type="checkbox" name="check_list[]" id="check" value="batch"></td>';
                    echo '<td><center> <input type="text" name="batch_no"><br>  </td>';
                    echo "<td><center> <input type='submit' name='submit' value='submit'></td>";
                    echo '<td><input type="hidden" name="job_number[result]" value="'."false".'"</td>'; 
                    echo '<td><input type="hidden" name="job_number[number]" value="'.$nJobNumber.'"</td>'; 
                }


                echo "</tr> </form>";
            }
        echo "</tbody> </table>";

      ?>

Insert values to database:

if(isset($_POST['submit']))
{
    $checkbox = $_POST["check_list"];
    $job_number = $_POST["job_number"];
    $batch_no = $_POST["batch_no"];
    date_default_timezone_set('Australia');
    $date_time = date("Y-m-d h:i:sa");
    //$username = "$SESSION[userid]";
    if($job_number["result"] == "true"){
        if(!empty($_POST['check_list'])) {
            $checkboxes = implode(",",$_POST['check_list']);
            $stmt = $link->prepare('UPDATE time_stamps SET time_date=?,username=?, batch_no=?,check_list=? WHERE nJobNumber=?');
            $stmt->bind_param('ssssi',$date_time,$username,$batch_no,$checkboxes,$job_number["number"]);
            $stmt->execute();
        }
    }else{
        if(!empty($_POST['check_list'])) {
            $checkboxes = implode(",",$_POST['check_list']);
            $stmt = $link->prepare('INSERT INTO time_stamps (nJobNumber,time_date,username,batch_no,check_list) VALUES (?,?,?,?,?)');
            $stmt->bind_param('issss',$job_number["number"],$date_time,$username,$batch_no,$checkboxes);
            $stmt->execute();
        }
    }

}




Aucun commentaire:

Enregistrer un commentaire