samedi 6 novembre 2021

POST checked AND unchecked checkbox status (PHP)

one of my sites contains a table. The information for this table comes from a database. Lets say the ID of each position and a boolean wich represents the checkbox and it loads the 0 or 1 from the database and "pre"-checks or unchecks the checkbox.

<input class='cb' name='check[]' value='".$buyedposition."' type='checkbox' "; ?> <?php if($buyedposition==1){echo "checked";} ?> <?php echo ">";

$buyedposition is the 0 or 1 information from the Database.

Now the user can change the checkbox by clicking each record that is shown in the table. The table is also a form. With a submit button the new values should POST to the next php file to update the records. But it doesn't work.

Only the checked checkboxes will pass through and the array size is = the number of checked checkboxes. I already tried a hidden input but the array size is incorrect. For 4 empty checkboxes the array size is 4. If I check all 4 the array size is 8.

var_dump of the array (all 4 checkboxes are checked):

array(8) { [0]=> string(0) "" [1]=> string(1) "0" [2]=> string(0) "" [3]=> string(1) "0" [4]=> string(0) "" [5]=> string(1) "0" [6]=> string(0) "" [7]=> string(1) "0" }

With "isset" it doesn't work either. (!isset($_POST["check"]) is true when none of all checkboxes are checked. I cant use it to ask "Okay Checkbox 1 and 3, are you checked?" I can use this only to update all checkboxes to 0 if the user has unchecked all boxes:

if(!isset($_POST["check"])){
        foreach($_POST["positionID"] as $key=>$value){
            $sql = $connection->prepare("UPDATE positionen SET buyed=? WHERE positionen.id = ?");
            $sql->bind_param("ii", $checktozero, $_POST["positionID"][$key]);
            $sql->execute(); 
        }

So is there any way to solve this problem?




Aucun commentaire:

Enregistrer un commentaire