Home > Back-end >  Cant get days and time in multiple array
Cant get days and time in multiple array

Time:09-11

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

I am not getting from time and to time when I submit, I can get either from time or to time only

CodePudding user response:

It works fine for me. But there may be confusion in your mind. Day is a checkbox, so if its not ticked, you dont get a day but you still get a time. If only one day is ticked, the array size is 1.

See modified code



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

CodePudding user response:

I have tried the above code I have selected Monday and Wednesday day but I got the below result

enter image description here

  • Related