如何在php codeigniter中获取输入字段的隐藏ID

时间:2016-09-16 05:54:07

标签: 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是变化的。如何在每个表单中提交student_ids。我通过$ class_id = $ this-&gt; input-&gt; post(&#39; class_id&#39;)在我的控制器中得到这个class_id;提前致谢。 我的控制器方法是

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

2 个答案:

答案 0 :(得分:2)

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

视图:

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

controlller ::

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

答案 1 :(得分:1)

    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>