Ajzz Ajzz - 2 months ago 13
PHP Question

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

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

Answer

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