搜索

首页  >  问答  >  正文

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

<p>我有一个组件,它从 MySQL 数据库中获取所有数据。</p>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<!--?php

 

函数组件($productName, $productPrice, $productImg, $productID)

{

    $元素=“   

            <div class='col-md-3 col-sm-6 my-3 my-md-0'-->;

            <form action="index.php" method="post" id="myform">;

                <div class="卡影">;

                    <img src="{$productImg}" alt="image1" class="img-fluid card-img-top">;

                    <div class="\“卡体\”">

                        <h5 class="card-title">{$productName}</h5>;

                    </div>

                    <p class='card-text'>此处显示信息 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> 

                    <输入类型='隐藏'名称='product_id'值='{$productID}'>

 

                </div>

            </表格>

        </form>

”; 回显$元素; }</pre> <p>我还有一个用于表单的提交按钮。 <code><button type="submit" name="submit" form="myform">显示所选内容</button></code></p> <p>获取容器的代码:</p>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<div class="容器">

    <div class="row text-center py-5">

        <!--?php

 

        $result = $database--->getData();

        while ($row = mysqli_fetch_assoc($result)) {

            组件($row['product_name'], $row['product_price'], $row['product_image'], $row['id']);

        }

 

        ?>

    </div></pre>

<p>当点击提交按钮时,用于检查容器是否被选中并返回其值(productID)的PHP代码。</p>

<pre class="brush:php;toolbar:false;">if (isset($_POST['提交'])) {

    if (!empty($_POST['checkid'])) {

        foreach ($_POST['checkid'] as $value) {

            回显“值:” 。 $价值。 '<br/>';

        }

    }

}</pre>

<p>我的数据库中有几个产品,但它只适用于第一个。其他的作用。</p></pre></div><div class="info mb-10"><a rel="nofollow" href=" https://m.php.cn/zh/member/1272252.html"><img src="https://www.php.cn/static/images/user_avatar.jpg" alt="P粉787820396"><span style="margin-left: 5px;">P粉787820396</span></a><span class="pl-10">545 天前</span><span class="pl-10">586 <b class="kclbcollectb"></b></span></div>

全部回复(1)我来回复

  • P粉511985082

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

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

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    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;

    }

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

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    <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
  • 取消回复