Home > Enterprise >  Isset only checks first checkbox from database
Isset only checks first checkbox from database

Time:02-18

I have a component that gets all data from the MySQL database.

<?php

function component($productName, $productPrice, $productImg, $productID)
{
    $element = "    
            <div class='col-md-3 col-sm-6 my-3 my-md-0'>
            <form action='index.php' method='post' id='myform'>
                <div class='card shadow'>
                    <img src='{$productImg}' alt='image1' class='img-fluid card-img-top'>
                    <div class=\"card-body\">
                        <h5 class='card-title'>{$productName}</h5>
                    </div>
                    <p class='card-text'>info goes here lorem ipsum</p>
                    <span class='price'>{$productPrice}</span>
                    <span class='price'>{$productID}</span>
                    <div class='form-check form-switch'>
                        <input class='form-check-input' type='checkbox' name='checkid[]' value='{$productID}'>
                    </div>  
                    <input type='hidden' name='product_id' value='{$productID}'>

                </div>
            </form>
        </div>

    
    ";
    echo $element;
}

I also have submit button for the form. <button type="submit" name="submit" form="myform">show selected</button>

code to get containers:

<div >
    <div >
        <?php

        $result = $database->getData();
        while ($row = mysqli_fetch_assoc($result)) {
            component($row['product_name'], $row['product_price'], $row['product_image'], $row['id']);
        }

        ?>
    </div>

And PHP code to check whether container is checked and return its value (productID) when submit button is clicked.

if (isset($_POST['submit'])) {
    if (!empty($_POST['checkid'])) {
        foreach ($_POST['checkid'] as $value) {
            echo "value : " . $value . '<br/>';
        }
    }
}

I have several products in the database, but it only works for first checkbox. Others don't do anything.

CodePudding user response:

you are looping the FORM and this is not true. You can change your function like

function component($productName, $productPrice, $productImg, $productID)
{
    $element = "    
                <div class='card shadow'>
                    <img src='{$productImg}' alt='image1' class='img-fluid card-img-top'>
                    <div class=\"card-body\">
                        <h5 class='card-title'>{$productName}</h5>
                    </div>
                    <p class='card-text'>info goes here lorem ipsum</p>
                    <span class='price'>{$productPrice}</span>
                    <span class='price'>{$productID}</span>
                    <div class='form-check form-switch'>
                        <input class='form-check-input' type='checkbox' name='checkid[]' value='{$productID}'>
                    </div>  
                    <input type='hidden' name='product_id' value='{$productID}'>

                </div>
    ";
    echo $element;
}

and code to get containers and submit button like

<div >
    <div >
<div class='col-md-3 col-sm-6 my-3 my-md-0'>
            <form action='index.php' method='post' id='myform'>
        <?php
        $result = $database->getData();
        while ($row = mysqli_fetch_assoc($result)) {
            component($row['product_name'], $row['product_price'], $row['product_image'], $row['id']);
        }
        ?>
<button type="submit" name="submit" form="myform">show selected</button>
            </form>
        </div>
    </div>
  • Related