"how to use checkboxes to enter true (1) or false (0) into mysql and show as checked in php / html form?" Code Answer

1

you need to give the checkboxes an explicit index in the html:

$i = 0;
while($shift_query = $result_shift_query->fetch_assoc())
    {
    echo "<tr>";    
    echo "<td><input type="hidden" name="shift_id[]"></td>";
    echo "<td><input type="text" name="shift_name[]"></td>";
    echo "<td><input type="text" name="shift_short_name[]"></td>";
    echo "<td><input type="text" name="shift_color[]"></td>";
    echo "<td><input type="checkbox" name="shift_trig[$i]"";
        if($shift_query['shift_trig'] == '1')
            {
            echo " checked="checked"";
            }
    echo " value="1"></td>";
    echo "<td><input type="checkbox" name="deletebox[$i]"></td>";
    echo "</tr>";
    $i++;
    }

in the update code, at the beginning of the loop, do:

    if (!isset($trig[$i])) { $trig[$i] = 0; }

the rest of the update code is unchanged.

By Araz Mohammadnejad on August 20 2022

Answers related to “how to use checkboxes to enter true (1) or false (0) into mysql and show as checked in php / html form?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.