CodeIgniter Forums
error occurred in getting checkbox value - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: error occurred in getting checkbox value (/showthread.php?tid=71670)



error occurred in getting checkbox value - kvanaraj - 09-11-2018

Code:
<input type="checkbox" id="mycheck2" name="certid[]"        
     value="2"  class="cbx" <?php echo set_checkbox($student2['certid'],'1',
     $student2['certid']==2);?> >

Code:
A PHP Error was encountered
Severity: Notice

Message: Undefined offset: 1

Filename: controllers/Users.php

Line Number: 258

Backtrace:

File: C:\xampp\htdocs\transcript2\application\controllers\Users.php
Line: 258
Function: _error_handler

File: C:\xampp\htdocs\transcript2\index.php
Line: 315
Function: require_once
i am using 7 checkbox . first checkbox value correctly inserted. remaining checkboxes wrongly inserted.
Code:
controller
$certids = $this->input->post('certid');  //here you leave the [ ] out!
    $nocs = $this->input->post('noc');
    $result = array();
    foreach ($certids as $index=>$certid)
 {
       $result[] = $certid . '_' . $nocs[$index];
    }
    $date = new DateTime("now");
    $today = $date->format('Y-m-d');

    foreach($result as $value)
       {
       list($certid,$noc) = explode ('_',$value);
        $insert = array();
       $insert[] = array(
           'appno' => $appno,
           'regno' => $regno,
          'certid' => $certid,
             'noc' => $noc,
             'date' => $today
               );      
     
       $this->load->model('user_Model');
       $this->User_Model->studreginsert($insert);        
      }



RE: error occurred in getting checkbox value - Pertti - 09-11-2018

That looks like code from view file, not from controller, however because you are using name="certid[]", it's likely that you are looping over 0-6 indexes, where POST variable only sends values that were checked, so it might only have 1 value in certid array, if only one box was ticked.

Check your POST data that comes in.


RE: error occurred in getting checkbox value - kvanaraj - 09-11-2018

(09-11-2018, 10:53 PM)Pertti Wrote: That looks like code from view file, not from controller, however because you are using name="certid[]", it's likely that you are looping over 0-6 indexes, where POST variable only sends values that were checked, so it might only have 1 value in certid array, if only one box was ticked.

Check your POST data that comes in.

controller file also attached.
output 
Array
(
   [0] => 1_2
   [1] => 2_2
   [2] => 3_2
   [3] => 4_2
   [4] => 5_2
   [5] => 6_
   [6] => 7_
)



RE: error occurred in getting checkbox value - Pertti - 09-12-2018

First, when you use input name="array[]", you can't really trust indexes, if you select first and third item from one array, and second and fourth item from second array, you get:

PHP Code:
$array1[
    
=> 'value for item 1',
    
=> 'value for item 3'
];

$array2[
    
=> 'value for item 2',
    
=> 'value for item 4'
]; 

So as fist thing, you ought to find a way to include related metadata (in your case, matching certid) somehow in the name/array that comes back.