1

I am using multiple HTML checkboxes to submit student attendance. The checkbox once clicked submit '1' and if not checked, will submit '0'. I am able to submit '1' when checked but cant submit '0'. Below is my code:

<?php
    $subcheck = (isset($_POST['subcheck'])) ? 1 : 0;
    $date = date("Y/m/d");
    foreach ( $student as $attendance ) {
              echo "<tr>";
              echo "<td>$attendance->id</td>";
                echo "<td>$attendance->name</td>";
                echo "<td>
                        $attendance->classroom_id
                      </td>";?>
                    <input type="hidden" name="date[]" value="<?php echo $date;?>" />

            <td><input type="checkbox" name="status[]" value="<?php echo $attendance->id;?>"></td> 
            <td><input type="text" name="reason[]" ></td>

      <?php
            }
            ?>
<tr>  
      <td colspan="2" align="center"><input type="submit" value="Save" name="submit"></td>

1 Answer 1

1

This might give you some ideas:

<?php
    $subcheck = (isset($_POST['subcheck'])) ? 1 : 0;
    $date = date("Y/m/d");

    $out = '<table id="tblAttendance"><thead><th>ID</th><th>Name</th><th>Room</th><th>Status</th><th>Reason</th></thead><tbody>';
    foreach ( $student as $attendance ) {
        $out .= '<tr>';
        $out .=     '<td>' .$attendance->id. '<input type="hidden" name="studentID[]" value="' .$attendance->id. '"></td>';
        $out .=     '<td>' .$attendance->name. '<input type="hidden" name="name[]" value="' .$attendance->name. '"></td>';
        $out .=     '<td>' .$attendance->classroom_id. '<input type="hidden" name="classroomID[]" value="' .$attendance->classroom_id. '"></td>';
        $out .=     '<td><input type="checkbox" name="status[]" value="yes"></td>';
        $out .=     '<td><input type="text" name="reason[]" ></td>';
        $out .= '</tr>';
    }
    $out .= '<tr><td colspan="2" align="center"><input type="submit" value="Save" name="submit"></td></tr>';
    $out .= '</tbody></table>';
    $out .= '<input type="hidden" name="date" value="' .$date. '" />';
    echo $out;
?>

Since you are using a type="submit" button, I presume you have this inside a form construct?

Recall how checkboxes work in HTML forms: if the box is checked, the value received on the PHP side will be the value="yes" value. In other words, the variable $_POST['status'][n] will have the value yes.

HOWEVER, if the checkbox is not set, then $_POST['status'][n] will not be set.

Reference:

http://www.html-form-guide.com/php-form/php-form-checkbox.html

Sign up to request clarification or add additional context in comments.

1 Comment

I think the question is not correctly understood. The code that you gave is doing the same thing without any change. I want value "0" to be sent when checkbox is not checked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.