0

Facing issue to get multiple checkbox array values in Codeignite

<div class="form-actions">
  <form action="candidates/posted" name="posted" method="post">
  <button class="btn btn-primary" type="submit" value="submit" name="submit">Action</button> 
</div>

      <table class="table table-striped table-bordered table-condensed">
        <thead>
          <tr>
            <th class="header">#</th>
            <th class="checkbox">Status</th>
            <th class="yellow header headerSortDown">Roll No</th>
            <th class="green header">Class Name</th>
            <th class="red header">Full Name</th>
            <th class="red header">Father Name</th>
            <th class="red header">Sections</th>
            <th class="red header">Actions</th>
          </tr>
        </thead>
        <tbody> 

<?php
    foreach($candidates as $row)
    {
        echo '<tr>';
        echo '<td>'.$row['id'].'</td>';
        //echo '<td>' .'</td>';
        echo '<td><input type="checkbox" class ="chkCheckBoxId" value='.$row['id'].' name="chk_id[]"/></td>';
        echo '<td>'.$row['roll_no'].'</td>';
        echo '<td>'.$row['class_name'].'</td>';
        echo '<td>'.$row['full_name'].'</td>';
        echo '<td>'.$row['father_name'].'</td>';
        echo '<td>'.$row['section_name'].'</td>';
        echo '<td class="crud-actions">
        <a href="'.site_url("admin").'/candidates/update/'.$row['id'].'" class="btn btn-info">view & edit</a>  
        <a href="'.site_url("admin").'/candidates/delete/'.$row['id'].'" class="btn btn-danger">delete</a>
        </td>';
        echo '</tr>';
    }
    ?>
</form>

Controller here:

public function posted()
{
    if(!empty($_POST['chk_id'])) {
        foreach($_POST['chk_id'] as $check) {
            echo $check; 
        }
    }
}
goto
  • 7,908
  • 10
  • 48
  • 58
Azeem Khan
  • 21
  • 3

2 Answers2

0

Just try like this inside foreach loop.There is conflict on using quotes.

 $id = $row['id'];
 echo '<td><input type="checkbox" class ="chkCheckBoxId" value="'.$id.'" name="chk_id[]"/></td>';

In codeigniter you can use $this->input->post() to get posted values.So following is a better way..

public function posted()
{
    if(!empty($this->input->post('chk_id')) {
        foreach($this->input->post('chk_id') as $check) {
            echo $check; 
        }
    }
}
Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19
0

Correct me on this but I believe that an unchecked checkbox does not get sent back to the server. I have always checked to see if a checkbox is checked by seeing if it is in the $_POST[] array (I tend to use method="post").

When I need an array of checkboxes, I actually give them their own names. If I am listing a group of products for users to choose, for example, I will give each checkbox a name that starts with "chk_" and then append the ID of the product. This way, during processing the data, all I have to do is loop through the $POST[] array and every object that starts with chk is a checkbox and the rest indicates which product they chose.

Menachem Bazian
  • 397
  • 2
  • 5
  • 18