dpn4073
2016-09-16 05:54
浏览 36
已采纳

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

I am doing a project with code igniter in which i have a form that contains a list of students.My form is as follows:

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

In my controller i need to get the hidden inputs. ie; class_id and student_id. class_id is same for all 's in the table. But student_id is varying. How can i get the student_ids in each form submit. Iam getting this class_id in my controller by $class_id = $this->input->post('class_id'); Thanks in advance. My controller method is

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

图片转代码服务由CSDN问答提供 功能建议

我正在使用代码点火器进行一个项目,其中我有一个包含学生列表的表单。我的表格是 如下:

 &lt; form action ='my_controller / my_method'method =“post”&gt; 
&lt; table&gt; 
&lt; thead&gt; 
&lt;  tr&gt; 
&lt; th&gt; Roll&lt; / th&gt; 
&lt; th&gt;名称&lt; / th&gt; 
&lt; th&gt;&lt; / th&gt; 
&lt; / tr&gt; 
&lt; / thead&gt; \  n&lt; tbody&gt; 
&lt;?php foreach($ students as $ row){?&gt; 
&lt; tr&gt; 
&lt; td&gt;&lt;?php echo $ row-&gt; roll;  ?&gt;&lt; / td&gt; 
&lt; td&gt;&lt;?php echo $ row-&gt; name;  ?&gt;&lt; / td&gt; 
&lt; input type =“hidden”name =“class_id”value =“&lt;?php echo $ row-&gt; class_id;?&gt;”  /&gt; 
&lt; input type =“hidden”name =“student_id”value =“&lt;?php echo $ row-&gt; student_id;?&gt;”  /&gt; 
&lt; td&gt;&lt; input type =“submit”value =“some value”class =“btn btn-info”&gt;&lt; / td&gt; 
&lt; / tr&gt; 
&lt; /  tbody&gt; 
&lt; / table&gt; 
&lt; / form&gt; 
   
 
 

在我的控制器中,我需要获取隐藏的输入。 即; class_id和student_id。 class_id对于表中的所有人都是相同的。 但是student_id是变化的。 如何在每个表单中提交student_ids。 我通过$ class_id = $ this-&gt; input-&gt; post('class_id')在我的控制器中得到这个class_id; 提前致谢。 我的控制器方法是

  function my_method(){
 $ class_id = $ this-&gt; input-&gt; post('class_id'); 
 $ student_id =  $这 - &GT;输入 - &GT;柱( 'student_id数据');  
 echo $ student_id;  
} 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douhaoqiao9304 2016-09-16 06:46
    已采纳

    You can use button or anchor tag instead of submit, and can pass the student_id and class_id as parameters to the method and can use them further in your function or method. For example, I am using anchor tag as submit:

    view:

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

相关推荐 更多相似问题