如何在PHP编码器中获得输入字段的隐藏id


how to get hidden id of an input field in php codeigniter

我正在做一个项目与代码点火器,我有一个包含学生列表的形式。我的表单如下:

<form action = 'my_controller/my_method' method = "post">
 <table>
   <thead>
     <tr>
       <th>Roll</th>
       <th>Name</th>
       <th></th>
     </tr>
    </thead>
     <tbody>
       <?php foreach ($students as $row) { ?>
       <tr>
          <td><?php echo $row->roll; ?></td>
          <td><?php echo $row->name; ?></td>
          <input type = "hidden" name = "class_id" value = "<?php echo $row->class_id; ?>" />
          <input type = "hidden" name = "student_id" value = "<?php echo $row->student_id; ?>" />
          <td><input type="submit" value="some value" class= "btn btn-info"></td>
       </tr>
      </tbody>
   </table>
</form>

在我的控制器我需要得到隐藏的输入。即;Class_id和student_id。表中所有的Class_id都是相同的。但是student_id是变化的。我怎样才能得到学生id在每个表单提交。我得到这个class_id在我的控制器$class_id = $this->input->post('class_id');提前感谢。控制器方法是

 function my_method(){ 
   $class_id = $this->input->post('class_id');
   $student_id = $this->input->post('student_id'); 
   echo $student_id; 
 }

您可以使用按钮或锚标记代替提交,并且可以将student_id和class_id作为参数传递给方法,并且可以在您的函数或方法中进一步使用它们。例如,我使用锚标记作为submit:

视图:

<table>
      <thead>
        <tr>
            <th>Roll</th>
            <th>Name</th>
            <th></th>
        </tr>
     </thead>
     <tbody>
       <?php foreach ($students as $row) { ?>
       <tr>
          <td><?php echo $row->roll; ?></td>
          <td><?php echo $row->name; ?></td>
          <input type = "hidden" name = "class_id" value = "<?php echo $row->class_id; ?>" />
          <input type = "hidden" name = "student_id" value = "<?php echo $row->student_id; ?>" />
          <td>
            <a href="<?php echo base_url();  . 'conroller/method/' . $row->class_id . '/' . $row->student_id ?>" class="btn btn-info" > submit</a>
          </td>
       </tr>
     </tbody>

控制器:

class conroller extends CI_controllers{
     function method($classId, $studentId){
            // use classId and studentId here
           echo 'class id = ' . $classId;
           echo 'student id = ' . $studentId;
     }
}

    Here is the example javascript function
    function setValues(class_id,student_id){
         document.getElementById("class_id").value = class_id;
        document.getElementById("student_id").value = student_id; 
        return true;
    }
    HTML changes
    <form action = 'my_controller/my_method' method = "post">
     <table>
       <thead>
         <tr>
           <th>Roll</th>
           <th>Name</th>
           <th></th>
         </tr>
        </thead>
         <tbody>
           <?php foreach ($students as $row) { ?>
           <tr>
              <td><?php echo $row->roll; ?></td>
              <td><?php echo $row->name; ?></td>
              <td><input type="submit" value="some value" onclick="javascript:return setValues(<?php echo $row->class_id; ?>,<?php echo $row->student_id; ?>);" class= "btn btn-info"></td>
           </tr>
<?php } ?>
          </tbody>
       </table>
       <input type = "hidden" name = "class_id" id="class_id" value = "" />
              <input type = "hidden" name = "student_id" id="student_id" value = "" />
    </form>