简体   繁体   中英

Php submit form checkbox with checked box

I have a db where read all information I read a optino and the stato 1 is confirmed and 0 not... I create a table where anyone can update, check or remove check and after the submit I update all data but in the submit I see only the "checked"... the other not..

$c_row = current($row);

if ($y > 1) {
    echo "<form name=salvo method=post action='dettaglio.php?tipo=1'>";

    $id = substr($c_row,0,strpos($c_row, '|'));
    $stato = substr($c_row,strpos($c_row, '|')+1,1);

    echo "<td class='tg-dett' align=center>";

    if ($stato == 1) {
        echo "<input type='checkbox' name='chkColor[]' value='$c_row' checked>";
    } else {
        echo "<input type='checkbox' name='chkColor[]' value='$c_row' >";
    }

    echo "</td>";

    for($i = 0; $i < count($_POST["chkColor"]); $i++)
    {
        if(trim($_POST["chkColor"][$i]) != "") {
            echo "chkColor $i = ".$_POST["chkColor"][$i]."<br>";
        }
    }
}

the output is only checked, If anyone remove a check don't appear to output

You can either check on the values after posting it on being empty or not set. Or you can use the hidden input trick like so:

<input type='hidden' name='chkColor[' . $row["id"] . ']' value='0'><input type='checkbox' name='chkColor[' . $row["id"] . ']' checked>

This way it will post all boxes but will add a different value to determine which is on and which is not on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM