0

I have some check boxes and if I check only one check box there's an error of undefined index but if I check all there's no error, so basically the index will be defined when there is some value in it, but I only need to check one box but every time I do an error shows up and I cant advance, please help

if (isset($_POST['save12'])){
$ins = $_POST['teacher'];
$subject = $_POST['subject'];
$room = $_POST['room'];
$strand = $_POST['strand'];
$section = $_POST['section'];

$m = $_POST['m'];
$w = $_POST['w'];
$f = $_POST['f'];
$t = $_POST['t'];
$th = $_POST['th'];
}

that's how I defined my checkboxes and this is my checkbox

 <?php
    include '../functions/database.php';
    $sql = "SELECT * FROM time order by 'time_start'";
    $query=mysqli_query($conn,$sql)or die(mysqli_error());

    while($row=mysqli_fetch_array($query)){
        $id=$row['timeID'];
        $start=date("h:i a",strtotime($row['time_start']));
        $end=date("h:i a",strtotime($row['time_end']));

    ?>
     <tr >
                            <td><?php echo $start."-".$end;?></td>
                            <td><input type="checkbox" id="check" name="m[]" value="<?php echo $id;?>" style="width: 20px; height: 20px;"></td>
                            <td><input type="checkbox" id="check" name="t[]" value="<?php echo $id;?>" style="width: 20px; height: 20px;"></td>
                            <td><input type="checkbox" id="check" name="w[]" value="<?php echo $id;?>" style="width: 20px; height: 20px;"></td>
                            <td><input type="checkbox" id="check" name="th[]" value="<?php echo $id;?>" style="width: 20px; height: 20px;"></td>
                            <td><input type="checkbox" id="check" name="f[]" value="<?php echo $id;?>" style="width: 20px; height: 20px;"></td>

                          </tr>

    <?php }?>

it works fine when I click all check boxes but if I only choose one the other 4 will be undefined, thanks in advance

dread
  • 51
  • 1
  • 8
  • 3
    Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – miken32 Feb 13 '19 at 05:41
  • When a checkbox isn't checked, it won't be set when the form is submitted - in other words, if you check the checkbox, it will appear in the `$_POST` array, if its not set, it won't appear. Since you have with different names, they will not be set unless checked. Do `$m = isset($_POST['m']) ? $_POST['m'] : null;` instead. You can also use the *Null Coalesce Operator* `??` (since PHP 7), and do `$m = $_POST['m'] ?? null;`. – Qirel Feb 13 '19 at 05:55
  • it works @Qirel thank you so much – dread Feb 13 '19 at 06:13

0 Answers0