<?php
if($_POST)
{
$day = $_POST['day'];
$from = $_POST['from'];
$to = $_POST['to'];
for($i=0;$i<count($day);$i++) {
$days = $day[$i];
$froms = $from[$i];
$tos = $to[$i];
echo 'From'.$froms.'<br> To'.$tos;
}
}
?>
<form action="" method="POST">
<input type="checkbox" name="day[]" value="Monday"> Monday | From <input type="time" name="from[]" to="from[]"> To<input type="time" name="to[]" to="to[]"><br>
<input type="checkbox" name="day[]" value="Tuesday"> Tuesday| From <input type="time" name="from[]" to="from[]"> To<input type="time" name="to[]" to="to[]"><br>
<input type="checkbox" name="day[]" value="Wednesday"> Wednesday | From <input type="time" name="from[]" to="from[]"> To<input type="time" name="to[]" to="to[]"><br>
<input type="submit" name="submit" id="submit" value="submit">
</form>
当我提交时,我不会时不时地得到,我可以时不时地得到或只得到
这对我来说很好。但你可能会感到困惑。一天是一个复选框,所以如果不勾选,你就没有一天,但你仍然有时间。如果只勾选了一天,则数组大小为1。
参见修改后的代码
<?php
if($_POST)
{
$day = $_POST['day'] ?? ''; //<---- if no day is selected this will be null
$from = $_POST['from'];
$to = $_POST['to'];
for($i=0;$i<2;$i++) { //<---- Using 2 here as the length of array days varies
$days = $day[$i] ?? ''; //<---- Similarly here
$froms = $from[$i];
$tos = $to[$i];
echo 'Day'.$days.' From'.$froms.' To'.$tos.'<br>';
}
}
?>
<form action="" method="POST">
<input type="checkbox" name="day[]" value="Monday"> Monday | From <input type="time" name="from[]" to="from[]"> To<input type="time" name="to[]" to="to[]"><br>
<input type="checkbox" name="day[]" value="Tuesday"> Tuesday| From <input type="time" name="from[]" to="from[]"> To<input type="time" name="to[]" to="to[]"><br>
<input type="checkbox" name="day[]" value="Wednesday"> Wednesday | From <input type="time" name="from[]" to="from[]"> To<input type="time" name="to[]" to="to[]"><br>
<input type="submit" name="submit" id="submit" value="submit">
</form>