mathew
mathew

Reputation: 82

how to get checked checkbox table row value in codeigniter

VIEW PAGE

<table>
<thead>
    <tr>
        <th><input type="checkbox" checked="checked" class="checkAll"  name="checkAll" /></th>
        <th>#</th>
        <th>Beneficiary Name</th>
        <th>Stipendiary Type</th>
        <th class="text-right box">Bonus ₹</th>
        <th class="text-right">Stipendiary ₹</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td align="center">
            <input type="checkbox" checked="checked" class="chkclass " name="bene_id[]" value="1" /><input type="hidden" name="amount[]" value="500" tabindex ="-1" />
        </td>
        <td>1</td>
        <td>Jeinbai Nesamony</td>
        <td>Poor Pension</td>
        <td class="text-right box" id="hideshow">
            <input type="text" name="bonus[]" id="bonus" value="" class="tbl-input-cus bonus" tabindex ="1" />
        </td>
        <td class="text-right wagein">500.00</td>
    </tr>
    <tr>
        <td align="center">
            <input type="checkbox" checked="checked" class="chkclass " name="bene_id[]" value="2" /><input type="hidden" name="amount[]" value="400" tabindex ="-1" />
        </td>
        <td>2</td>
        <td>Chellammal Kochimoni</td>
        <td>Poor Aid</td>
        <td class="text-right box" id="hideshow">
            <input type="text" name="bonus[]" id="bonus" value="" class="tbl-input-cus bonus" tabindex ="1" />
        </td>
        <td class="text-right wagein">400.00</td>
    </tr>
    <tr>
        <td align="center">
            <input type="checkbox" checked="checked" class="chkclass " name="bene_id[]" value="3" /><input type="hidden" name="amount[]" value="400" tabindex ="-1" />
        </td>
        <td>3</td>
        <td>Thasammal Thangaiah</td>
        <td>Poor Aid</td>
        <td class="text-right box" id="hideshow">
            <input type="text" name="bonus[]" id="bonus" value="" class="tbl-input-cus bonus" tabindex ="1" />
        </td>
        <td class="text-right wagein">400.00</td>
    </tr>
    <tr>
        <td align="center">
            <input type="checkbox" checked="checked" class="chkclass " name="bene_id[]" value="4" /><input type="hidden" name="amount[]" value="400" tabindex ="-1" />
        </td>
        <td>4</td>
        <td>Roselet</td>
        <td>Poor Aid</td>
        <td class="text-right box" id="hideshow">
            <input type="text" name="bonus[]" id="bonus" value="" class="tbl-input-cus bonus" tabindex ="1" />
        </td>
        <td class="text-right wagein">400.00</td>
    </tr>
    <tr>
        <td align="center">
            <input type="checkbox" checked="checked" class="chkclass " name="bene_id[]" value="5" /><input type="hidden" name="amount[]" value="400" tabindex ="-1" />
        </td>
        <td>5</td>
        <td>Kamalam Chellam R.</td>
        <td>Poor Aid</td>
        <td class="text-right box" id="hideshow">
            <input type="text" name="bonus[]" id="bonus" value="" class="tbl-input-cus bonus" tabindex ="1" />
        </td>
        <td class="text-right wagein">400.00</td>
    </tr>
</tbody>

enter image description here

MY REQUIREMENT

I want to save bellowed data's to table
1. bene_id
2. Bonus ₹
3. Stipendiary ₹

I've fetch this table data form existing Beneficiary Table. So Bene_id and Stipendiary ₹ value get from that tabel. Bonus ₹ will be an input.
Now i want to save table data to the payment table.
I'm trying to post the value by array. it's working fine.
now i've an issue with the check box. i want to neglect the row value that unchecked. That means i want row value which was checkbox : checked

i'm expecting jquery for passing checkbox : checked row value to hidden input array.

Upvotes: 0

Views: 1588

Answers (1)

Hardood
Hardood

Reputation: 523

As i told you in the comments section, you can use normal HTML forms to submit to the action method on your controller, but you need to modify your form a little bit, this is the easiest solution.

Despite of option one simplicity i decided to give you another approach to solve this problem, so first look at the code of HTML and JavaScript:

<table>
<thead>
   <tr>
     <th><input type="checkbox" checked="checked" class="checkAll"  name="checkAll" /></th>
     <th>#</th>
     <th>Beneficiary Name</th>
     <th>Stipendiary Type</th>
     <th class="text-right box">Bonus ₹</th>
     <th class="text-right">Stipendiary ₹</th>
   </tr>
</thead>
<tbody id="details">
  <tr>
    <td align="center">
        <input type="checkbox" checked="checked" class="chkclass " id="bene_id" value="1" />
    </td>
    <td>1</td>
    <td>Jeinbai Nesamony</td>
    <td>Poor Pension</td>
    <td class="text-right box" id="hideshow">
    <input type="text" name="bonus" id="bonus" value="" class="tbl-input-cus bonus" tabindex ="1" />
    </td>
    <td class="text-right wagein" id="amount">500.00</td>
</tr>
<tr>
    <td align="center">
        <input type="checkbox" checked="checked" class="chkclass " id="bene_id" value="2" />
    </td>
    <td>2</td>
    <td>Chellammal Kochimoni</td>
    <td>Poor Aid</td>
    <td class="text-right box" id="hideshow">
        <input type="text" name="bonus" id="bonus" value="" class="tbl-input-cus bonus" tabindex ="1" />
    </td>
    <td class="text-right wagein" id="amount" >400.00</td>
</tr>
<tr>
    <td align="center">
        <input type="checkbox" checked="checked" class="chkclass " id="bene_id" value="3" />
    </td>
    <td>3</td>
    <td>Thasammal Thangaiah</td>
    <td>Poor Aid</td>
    <td class="text-right box" id="hideshow">
        <input type="text" name="bonus" id="bonus" value="" class="tbl-input-cus bonus" tabindex ="1" />
    </td>
    <td class="text-right wagein" id="amount" >400.00</td>
</tr>
<tr>
    <td align="center">
        <input type="checkbox" checked="checked" class="chkclass " id="bene_id" value="4" />
    </td>
    <td>4</td>
    <td>Roselet</td>
    <td>Poor Aid</td>
    <td class="text-right box" id="hideshow">
        <input type="text" name="bonus" id="bonus" value="" class="tbl-input-cus bonus" tabindex ="1" />
    </td>
    <td class="text-right wagein" id="amount">400.00</td>
</tr>
<tr>
    <td align="center">
        <input type="checkbox" checked="checked" class="chkclass " id="bene_id" value="5" />
    </td>
    <td>5</td>
    <td>Kamalam Chellam R.</td>
    <td>Poor Aid</td>
    <td class="text-right box" id="hideshow">
        <input type="text" id="bonus" value="" class="tbl-input-cus bonus" tabindex ="1" />
    </td>
    <td class="text-right wagein" id="amount">400.00</td>
</tr>
</tbody>
</table>

<button id="submit">Submit</button>

<script type="text/javascript" src="<?= base_url(assets/js/jquery.min.js) ?>"></script>
<script type="text/javascript">
  function jsonify(){

    var rows = $('#details tr');
    var a = [];
    rows.each(function(){

        if($(this).find('#bene_id').is(':checked'))
        {
           var bene_id       = $(this).find('#bene_id').val();
           var stipendiary   = $(this).find('#amount').html();
           var bonus         = $(this).find('#bonus').val();

           var x = {
                bene_id:bene_id,
                stipendiary:stipendiary,
                bonus:bonus
            };
            a.push(x);
        }
    });
    var c = JSON.stringify(a);
    return c;
}

 $(function(){

   $('#submit').click(function(){
       $data = jsonify();

       $.ajax({
          type:'POST',
          url:'<?= base_url('controller/method_name') ?>',
          data:{details:data},
          success:function(response)
          {
            //if you data save successfuly, do sth here..
          }
       });
    });

 });

The following code is a PHP code of the action method on the specified controller:

   public function method_name()
   {
         $details  = json_decode($this->input->post('details'));

         foreach($details as $det ){

            $bene_id     = $det->bene_id;
            $stipendiary = $det->stipendiary;
            $bonus       = $det->bonus;

            // your logic goes here
         }
   }

In this solution i didn't considered the validation and security issues, because i wanted to make it simple, so before you put it in your production server you must deal with these issues.

I hope it helps.

Upvotes: 1

Related Questions