猿问

如何验证复选框集/复选框数组?

在互联网上寻找了多年之后,我想我想在这里寻求帮助。我这里有一个简单的复选框表,我在其中单击复选框,结果以 PHP 形式发回,但是当我不单击任何复选框并提交时,我收到以下错误。


Notice: Undefined index: idlights in C:\xampp\htdocs\lt4\checkbox\index.php on line 44


Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\lt4\checkbox\index.php on line 44

我如何验证此表,以便如果未选中任何内容,它只会显示类似“请选中复选框”或类似内容的内容?我试过了


if (empty($camid)


{


echo "<p style='color: red'>Please check a checkbox </p>";

}

但这没有用,这是我的代码,非常感谢对此的任何帮助


<?php

            include('conn.php');

                $query=mysqli_query($conn,"select * from `camera`");

                while($row=mysqli_fetch_array($query)){

                    ?>

                    <tr>

                        <td><input type="checkbox" value="<?php echo $row['camid']; ?>" name="camid[]"></td>

                        <td><?php echo $row['cameras']; ?></td>



                    </tr>

                    <?php

                }

            ?>


        </tbody>

    </table>

    <br>

    <input type="submit" name="submit" value="Submit">

    </form>

    </div>

    <div>

        <h2>You Booked:</h2>

        <?php

            if (isset($_POST['submit'])){

                foreach ($_POST['camid'] as $id):



                $sq=mysqli_query($conn,"select * from `camera` where camid='$id'");

                $srow=mysqli_fetch_array($sq);

                echo $srow['cameras']. "<br>";


                endforeach;

            }

        ?>

       ```


梵蒂冈之花
浏览 75回答 2
2回答

料青山看我应如是

你使用任何框架吗?尝试在你的复选框中添加必需的<input&nbsp;type="checkbox"&nbsp;value="<?php&nbsp;echo&nbsp;$row['camid'];&nbsp;?>"&nbsp;name="camid[]"&nbsp;required>

慕标琳琳

只需使用如下所示的附加 if 语句。<?php&nbsp; &nbsp; &nbsp; &nbsp; if (isset($_POST['submit'])){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isset($_POST['camid'])){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;foreach ($_POST['camid'] as $id):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sq=mysqli_query($conn,"select * from `camera` where camid='$id'");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $srow=mysqli_fetch_array($sq);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $srow['cameras']. "<br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endforeach;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ?>
随时随地看视频慕课网APP
我要回答