在互聯網上尋找了多年之后,我想我想在這里尋求幫助。我這里有一個簡單的復選框表,我在其中單擊復選框,結果以 PHP 形式發回,但是當我不單擊任何復選框并提交時,我收到以下錯誤。Notice: Undefined index: idlights in C:\xampp\htdocs\lt4\checkbox\index.php on line 44Warning: 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; } ?> ```
2 回答

料青山看我應如是
TA貢獻1772條經驗 獲得超8個贊
你使用任何框架嗎?嘗試在你的復選框中添加必需的
<input type="checkbox" value="<?php echo $row['camid']; ?>" name="camid[]" required>

慕標琳琳
TA貢獻1830條經驗 獲得超9個贊
只需使用如下所示的附加 if 語句。
<?php
if (isset($_POST['submit'])){
if(isset($_POST['camid'])){
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;
}
}
?>
- 2 回答
- 0 關注
- 91 瀏覽
添加回答
舉報
0/150
提交
取消