mercredi 16 février 2022

Php with checkbox and array [duplicate]

Here's some sample of my code:

SQL Request which populate checkbox:

$sql = "SELECT * FROM tab WHERE name='$name'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    ?>
    <form action="" name="showitems" method="post">
    <?php
while($row = $result->fetch_assoc()) {
    ?>
            <p>"<?php echo $name. " " .$row['description'];?>" <input type="checkbox" name="item[]" value=<?php echo $row['numitem'];?> /></p> 
    <?php
    
    }  
    ?>
    </form>
<?php
 
} else {
    echo "0 results";
    }

then on submit button I try to fetch all checked button to do some SQL tasks:

if(isset($_POST['item]){
    foreach($_POST['item'] as $currentitem) {
     //do some stuff
    }

} 

but it never goes to the foreach, try to echo some stuff and it does work.




Aucun commentaire:

Enregistrer un commentaire