isset函数仅适用于数据库中的第一个复选框
P粉787820396
P粉787820396 2023-09-05 22:42:25
[MySQL讨论组]

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

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

我还有一个用于表单的提交按钮。

获取容器的代码:

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

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

        ?>
    </div>

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

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

我在数据库中有几个产品,但它只适用于第一个复选框。其他的不起作用。

P粉787820396
P粉787820396

全部回复(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>
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号