php -如何在表格中显示选中的选项?

bzzcjhmw  于 2022-12-10  发布在  PHP
关注(0)|答案(1)|浏览(132)

i have a table with select boxes. i want to display all of selected options same as the table (like matrix, maybe). but it is just displaying one column at the bottom. i've been looking for the solutions for hours but it seems like i need to ask this one here. any suggestion would be great. so, here is the code:
`

<style>
    table, th, td {
  border: 1px solid;
}
</style>

<table>
    <thead>
        <tr>
            <td></td>
            <td>C1</td>
            <td>C2</td>
            <td>C3</td>
        </tr>
    </thead>
    <tbody>
        <form action="" method="POST" id="formid">
            <?php
            for ($i=1; $i <= 3 ; $i++) {
                ?>
            <tr>
                <td>A<?php echo $i ?></td>

                <?php
                
                for ($k=1; $k <= 3 ; $k++) {
                ?>

                <td>
                    <select name="selectid[]">
                        <option disabled selected>-Select-</option>
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                    </select>
                    <?php } } ?>
                </td>
            </tr>
        </form>
    </tbody>
</table>
        <input type="submit" form="formid" name="submit" value="Submit">


<!-- display selected options -->
<?php if (isset($_POST['submit'])) { ?>
<style>
    table, th, td {
  border: 1px solid;
}
</style>

<table>
    <thead>
    <tr>
        <td>
            <td>C1</td>
            <td>C2</td>
            <td>C3</td>
        </td>
    </tr>
    </thead>
    <tbody>

        <?php
            for ($i=1; $i <= 3 ; $i++) { ?>
            <tr>
                <td>A<?php echo $i ?></td>

                <?php
            }
                $select = $_POST['selectid'];
                foreach ($select as $value) {
                ?>
            
            <td><?php echo $value ?></td>
        <?php } ?>
            </tr>
        
    </tbody>
</table>

<?php } ?>

`

68de4m5k

68de4m5k1#

代码中有几个问题,第一个问题是post请求只是将数据推送到数组中,而没有真正确认这些数据来自页面中的什么地方。您可以严格识别二维数组中的索引(看看表是如何二维的)。
此外,您没有在第一个循环中包括该行,该循环将所有数据推送到最后一行。下面是一个大致可行的示例,它实现了您想要的操作。

<style>
    table, th, td {
  border: 1px solid;
}
</style>

<table>
    <thead>
        <tr>
            <td></td>
            <td>C1</td>
            <td>C2</td>
            <td>C3</td>
        </tr>
    </thead>
    <tbody>
        <form action="" method="POST" id="formid">
            <?php
            for ($i=0; $i < 3 ; $i++) {
                ?>
            <tr>
                <td>A<?php echo $i ?></td>

                <?php
                
                for ($k=0; $k < 3 ; $k++) {
                ?>

                <td>
                    
                    <select name="selectid[<?php echo $i ?>][<?php echo $k ?>]">
                        <option disabled selected>-Select-</option>
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                    </select>
                    <?php } } ?>
                </td>
            </tr>
        </form>
    </tbody>
</table>
        <input type="submit" form="formid" name="submit" value="Submit">


<!-- display selected options -->
<?php if (isset($_POST['submit'])) { ?>
<style>
    table, th, td {
  border: 1px solid;
}
</style>

<table>
    <thead>
    <tr>
        <td>
            <td>C1</td>
            <td>C2</td>
            <td>C3</td>
        </td>
    </tr>
    </thead>
    <tbody>
        <?php
        for ($i=0; $i < 3 ; $i++) {
            ?>
        <tr>
            <td>A<?php echo $i ?></td>

            <?php
            
            for ($k=0; $k < 3 ; $k++) {
            ?>

            <td>
                <?php 
                if(isset($_POST['selectid'][$i][$k])){
                    echo $_POST['selectid'][$i][$k];
                }
                ?>
                <?php } } ?>
            </td>
        </tr>

        
        
    </tbody>
</table>
<?php 
} ?>

相关问题