mercredi 18 mai 2016

Working with checkbox from form array in php

Am looping products out from product table to add them to cart table. only the selected product by checking the checkbox should be added, but if you select, the selected ones do not correspond.. HERE IS THE HTML GETTING THE PRODUCTS OUT

<html>     
    <form action="#" id="" class="horizontal-form" method="post">
    <?php
    $LISTP = "SELECT * FROM products ORDER BY id";          

$sn = 0;
                    $stmt = $pdo->prepare($LISTP);
                    $stmt->execute();

                    while($list = $stmt->fetch(PDO::FETCH_ASSOC)){
                    $sn = $sn + 1;
                    $ID = $list['id'];
                    $NAME = $list['name'];
                   ?>
                     <input type="checkbox" name="slected[]" class="checkboxes" value="1" /> 
                     <input type="hidden" name="productid[]" class="" value="<?php echo $ID;?>" />
                     <input type="text" name="name[]" class="" value="<?php echo $NAME;?>" />
                    <?php }?> </form>

<?php 
// now when we submot the form
$slected = $_POST['slected'];
$prod = $_POST['productid'];
$name = $_POST['name'];

foreach($prod as $key => $product){
if($slected[$key]>0){

echo $product.' '.$name[$key].' '.@$slected[@$key].'--<br>';
}

// the problem is here, if you check all product it will work well, but if            you check the second one
// it would echo the second one giving it the name of the first one which     was not checked at all 
?>




Aucun commentaire:

Enregistrer un commentaire