搜索

首页  >  问答  >  正文

isset函数仅适用于数据库中的第一个复选框

<p>我有一个组件,它从 MySQL 数据库中获取所有数据。</p>
;
            
;
; image1;
{$productName}
;
<p class='card-text'>此处显示信息 lorem ipsum</p> {$productPrice}; {$productID};
; ;
<输入类型='隐藏'名称='product_id'值='{$productID}'>
</表格>
”; 回显$元素; }</pre> <p>我还有一个用于表单的提交按钮。 <code><button type="submit" name="submit" form="myform">显示所选内容</button></code></p> <p>获取容器的代码:</p>
<div class="row text-center py-5"> getData(); while ($row = mysqli_fetch_assoc($result)) { 组件($row['product_name'], $row['product_price'], $row['product_image'], $row['id']); } ?> </div></pre> <p>当点击提交按钮时,用于检查容器是否被选中并返回其值(productID)的PHP代码。</p>
if (isset($_POST['提交'])) {
    if (!empty($_POST['checkid'])) {
        foreach ($_POST['checkid'] as $value) {
            回显“值:” 。 $价值。 '<br/>';
        }
    }
}</pre>
<p>我的数据库中有几个产品,但它只适用于第一个。其他的作用。</p>
P粉787820396P粉787820396540 天前580

全部回复(1)我来回复

  • P粉511985082

    P粉5119850822023-09-06 00:24:09

    您每次循环都创建一个新的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>

    回复
    0
  • 取消回复