mardi 3 août 2021

in php how to save unchecked checkboxes

The below code is perfectly working fine to save checked checkboxes records. However, I also want to save the records of unchecked checkboxes. currently it is saving 0 if checked and I want if unchecked save 1. Please help

<?php

public function collectMaintenance($flatnum, $amount, $month, $status)
{
    $sql = "INSERT INTO payment values ('','$flatnum','$amount','$month','$status')";
    mysqli_query($this->connect(), $sql) or die(mysqli_error());
}

if (isset($_POST['submit'])) {

        $dt = date('F Y');
        $checked_arr = $_POST['paid'];
        if (isset($_POST['paid'])) {
            foreach ($_POST['flatnumber'] as $k => $v) {
                $flat_number = $_POST['flatnumber'][$k];
                $amount = $_POST['amount'][$k];
                if (in_array($_POST['flatnumber'][$k], $checked_arr)) {
                    $this->collectMaintenance($flat_number, $amount, $dt, 0);
                }

            }
        } 
    }
echo "<form method='post' id='collection' name='collection' enctype='multipart/form-data'>";
echo "<input type='checkbox' id='select-all' name='select-all' value='selectall'> Select All 
 &nbsp";
echo "<input type='submit' name='submit' value='SAVE'>";
echo '<hr/>';
$dt = date('F Y');
$number = 1;
echo "<hr/>";
foreach ($getFlat as $flatRecord) {

echo $number . ": ";
echo "<input type='checkbox' name='paid[]'     
value='$flatRecord[blocknum]-$flatRecord[flatnum]'>";
echo "<input type='hidden' name='flatnumber[]' 
value='$flatRecord[blocknum]-$flatRecord[flatnum]'>";

echo $flatRecord['blocknum'] . "-" . $flatRecord['flatnum'] . " amount of Rs. 2,000 ";
echo "<input type='hidden' value='2000' readonly size='5' name='amount[]'> ";
echo "for the month of " . $dt;

echo '<hr/>';
$number++;
}
echo "<input type='submit' name='submit' value='SAVE'>";
echo "</form>";

?>



Aucun commentaire:

Enregistrer un commentaire