0

我正在尝试使用 codeigniter 制作一个学校出勤跟踪器。我不知道如何让 Student_no 和单选按钮值 Present 或 Absent 进入数据库。student_no 和单选按钮的值都作为 NULL 发布到我的控制器?

我的控制器文件

function insertAttendance(){

    $student_no=$this->input->post('student_no');
    $attendance=$this->input->post('attendance');

    $data = array(
        'student_no'=>$student_no,
        'attendance'=>$attendance
        );
    //$this->form_validation->set_data($data);

    $this->db->set($data);
    $this->db->insert('attendance',$data);
}

我的观点

     <h3>ATTENDANCE TRACKER</h3>
                <table class="table table-lg" name="student_no"> 

                  <tr>
                  <th>Student_NO</th>
                  <th>Student name</th>
                  <th>Student DOB</th>
                  <th>Attendance</th>
                  </tr>

                <?php foreach ($query->result_array() as $row): {?>
                    <tr>
                        <td><?php echo $row['student_no'];?></td>

                        <td><?php echo $row['student_name'];?></td>

                        <td><?php echo $row['student_dob'];?></td>
                <tr>
                    <label>

<label><input type="radio" name="attendance[<?php echo $row['player_id']; ?>]"  value="Yes">Present</label> 
    &emsp;
<label><input type="radio" name="attendance[<?php echo  $row['player_id']; ?>]" value="No">Absent</label>

                    </td> </tr>

                    <?php } ?>

                    <?php endforeach; ?>

                    </tr>
                    </tbody>
                    </table>
4

1 回答 1

0

student_no 的表单域在哪里?我没有看到它,它应该抛出一个未定义的通知。假设 $row['player_id'] 不为空,则 $this->input->post('attendance') 将不存在。

<input type="radio" name="attendance_<?php echo $row['player_id']; ?>" value="Yes">

也许像这样尝试并为 student_no (隐藏字段?)创建一个表单字段。

你不需要

$this->db->set($data);

因为您已经在构建器的插入部分中传递了 $data 数组。

编辑:
我更新了输入字段代码。将表单标签放在 foreach 语句中,以便每个学生都有自己的表单。

每个表单都需要一个隐藏字段来传递 player_id。

<input type="hidden" name="player_id" value="<?php echo $row['player_id']; ?>">

在控制器中,验证 POST 数据并使用您传递的 player_id:

$this->input->post('attendance_'. $this->input->post('player_id'))

或类似的

编辑 2:
您也可以将<form>标签放在 foreach 语句之外,但是我们需要遍历控制器中的所有 POST 数据。这允许您在整个表单上有一个提交按钮,但确实使您的后端工作更加复杂。

于 2017-03-28T10:27:44.143 回答