Die isset-Funktion funktioniert nur beim ersten Kontrollkästchen in der Datenbank
P粉787820396
P粉787820396 2023-09-05 22:42:25
0
1
523
<p>我有一个组件,它从MySQL数据库中获取所有数据.</p> <pre class="brush:php;toolbar:false;"><?php Funktionskomponente($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='cardshadow'> <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 geht hierher 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; }</pre> <p>我还有一个用于表单的提交按钮. <code><button type="submit" name="submit" form="myform">显示所选内容</button></code></p> <p>获取容器的代码:</p> <pre class="brush:php;toolbar:false;"><div class="container"> <div class="row text-center py-5"> <?php $result = $database->getData(); while ($row = mysqli_fetch_assoc($result)) { Komponente($row['product_name'], $row['product_price'], $row['product_image'], $row['id']); } ?> </div></pre> <p> <pre class="brush:php;toolbar:false;">if (isset($_POST['submit'])) { if (!empty($_POST['checkid'])) { foreach ($_POST['checkid'] as $value) { echo "Wert: " . $value . '<br/>'; } } }</pre> <p>
P粉787820396
P粉787820396

Antworte allen(1)
P粉511985082

您每次循环都创建一个新的form元素。 您可以将您的函数更改为

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;
}

以及在一个单独的表单中获取您的组件(上面的函数)和提交按钮的代码,如下:

<div class="container">
    <div class="row text-center py-5">
<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>
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!