• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problems in insert checkbox value and corresponding text value.

#1
I want to insert multiple checkbox value and corresponding textbox value in to database. some times its working well most of the time the textbox value inserted as 0
i try lot of method . 
input form
Code:
checkbox
<input type = "checkbox" id="mycheck1" name="certid[]">
<input type = "checkbox" id="mycheck2" name="certid[]">
<input type = "checkbox" id="mycheck3" name="certid[]">
<input type = "checkbox" id="mycheck4" name="certid[]">
<input type = "checkbox" id="mycheck5" name="certid[]">

textitem
<input type="text"  name="noc1[]" id="eprimaryincome1" size="2" value="850"  >
<input type="text"  name="noc1[]" id="eprimaryincome2" size="2" value="300"  >
<input type="text"  name="noc1[]" id="eprimaryincome3" size="2" value="300"  >
<input type="text"  name="noc1[]" id="eprimaryincome4" size="2" value="300"  >
<input type="text"  name="noc1[]" id="eprimaryincome5" size="2" value="300"  >
method 1
Code:
$date = new DateTime("now");
 $today = $date->format('Y-m-d');
   $Specilized_category = $this->input->post('certid');
   $amt = $this->input->post('txt');
   $data=array('appno' =>$appno,'regno' =>$regno ,
   'certid'=>json_encode(implode(",", $Specilized_category)),
    'noc' => 1 ,
    'date' => $today ,
    'amt' => $amt
);
$this->db->insert('appdet', $data);

method 2
Code:
$certid = $this->input->post('certid');
 $count = count($certid);
 $amt = $this->input->post('txt',TRUE);
 $date = new DateTime("now");
 $today = $date->format('Y-m-d');
 $noc=1;
 $data =array();
 for($i=0; $i<$count; $i++) {
 $data[$i] = array(
          'appno' => $appno,
          'regno' => $regno,
          'certid' => $certid[$i],
          'noc' => $noc,
          'date'=> $today,
          'amt'=>$amt[$i]

          );
method 3
Code:
if(isset($_POST['certid']) && $_POST['certid']!="")
{
 

 $certids = $this->input->post('certid');  //here you leave the [ ] out!
 $nocs = $this->input->post('noc1');
 //print_r($nocs);
 $amtt = $this->input->post('txt');  

  $field1_array = isset($_POST['certid']) ? $_POST['certid'] : array();
   $field2_array = isset($_POST['noc1']) ? $_POST['noc1'] : array();
   $field3_array = isset($_POST['txt']) ? $_POST['txt'] : array();

   $total_rows = count($field1_array);

   if ($total_rows > 0)
   {
       for ($i=0; $i<$total_rows; $i++)
       {
       $field1_val = $field1_array[$i];
       $field2_val = $field2_array[$i];
       $field3_val = $field3_array[$i];

       $date = new DateTime("now");
 $today = $date->format('Y-m-d');

        $insert = array();
       $insert[] = array(
         'appno' => $appno,
         'regno' => $regno,
          'certid' => $field1_val,
             'noc' => 1,
             'date' => $today,
             'amt' => $field3_val
               );      
     
       $this->load->model('user_Model');
       $this->User_Model->studreginsert($insert);            
       }
   }
method 4
Code:
$certids = $this->input->post('certid');  //here you leave the [ ] out!
$nocs = $this->input->post('noc');
$nocs = isset($_POST['noc'][$i]) ? 1 : 0;
$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
               );      
all methods working well but some times. that amt inserted as '0'. 
Code:
$value = $this->input->post('formvalue', TRUE);
this can also be not worked. Kindly help me to solve my problem .


Attached Files Thumbnail(s)
   
Reply

#2
What is throwing you off might be a missing piece of knowledge about checkboxes. If a checkbox is unchecked when its form is submitted, then no value is submitted to the server to represent its unchecked state and the value is not submitted to the server at all.

In other words, only checked boxes are sent to the server.

In the screenshot you attached there are five checked boxes. If you get the posted values based on that screenshot with this code
PHP Code:
$certids $this->input->post('certid');
$nocs $this->input->post('noc1'); 
Then the $certids array will only have five items, but the $nocs array will have seven items.

You need a way to figure out which text input is linked to the posted checkboxes. To do that use the checkbox value attribute. The checkbox value is never seen on the client-side, but on the server this is the value given to the data submitted with the checkbox's name. By adding a value to each checkbox that represents the index in the $certids array you can determine which text input is linked to each checkbox.

(Interesting extra info: If you don't supply a checkbox value then the data submitted with a checked box will be the string "on".)

For this demo, I'm changing the text input values so that it is clear that we are getting the correct text input linked to each checkbox. Here's that code.

PHP Code:
<input type="text"  name="noc1[]" id="eprimaryincome1" size="2" value="850"  
<
input type="text"  name="noc1[]" id="eprimaryincome2" size="2" value="100"  
<
input type="text"  name="noc1[]" id="eprimaryincome3" size="2" value="200"  
<
input type="text"  name="noc1[]" id="eprimaryincome4" size="2" value="300"  
<
input type="text"  name="noc1[]" id="eprimaryincome5" size="2" value="400"  >
<
input type="text"  name="noc1[]" id="eprimaryincome6" size="2" value="500"  >
<
input type="text"  name="noc1[]" id="eprimaryincome7" size="2" value="600"  

Change the checkbox inputs to this.
PHP Code:
<input type "checkbox" id="mycheck1" name="certid[]" value="0">
<
input type "checkbox" id="mycheck2" name="certid[]" value="1">
<
input type "checkbox" id="mycheck3" name="certid[]" value="2">
<
input type "checkbox" id="mycheck4" name="certid[]" value="3">
<
input type "checkbox" id="mycheck5" name="certid[]" value="4">
<
input type "checkbox" id="mycheck6" name="certid[]" value="5">
<
input type "checkbox" id="mycheck7" name="certid[]" value="6"

Now consider this code.

PHP Code:
$certids $this->input->post('certid');
$nocs $this->input->post('noc1'); 
var_dump($certids);
var_dump($nocs); 

If you check all boxes except the third and sixth (like your screenshot shows) this is the var_dump output.

Quote:array (size=5)
 0 => string '0' (length=1)
 1 => string '1' (length=1)
 2 => string '3' (length=1)
 3 => string '4' (length=1)
 4 => string '6' (length=1)

array (size=7)
 0 => string '850' (length=3)
 1 => string '100' (length=3)
 2 => string '200' (length=3)
 3 => string '300' (length=3)
 4 => string '400' (length=3)
 5 => string '500' (length=3)
 6 => string '600' (length=3)

Now it is easy to link a checked box with its text input. Simply use a value from a $certids item as the index into $nocs. For instance, you can print out the text values linked to each checked box like this.

PHP Code:
foreach($certids as $selected)
{
   echo "certid[".$selected."] = ".$nocs[$selected]."<br>"; 


The screen output will be.
Quote:certid[0] = 850
certid[1] = 100
certid[3] = 300
certid[4] = 400
certid[6] = 600

I hope this is clear and you can use the ideas here to construct the data structure you need to save to the database. I'd offer more concrete code for that, but I'm not at all sure exactly what you are trying to save. An explanation of the table schema would help my understanding.
Reply

#3
I try your solution , but the output should be
certid[1] = 300
certid[2] = 300
certid[3] =
0
Reply

#4
(04-16-2019, 07:13 AM)kvanaraj Wrote: I try your solution , but the output should be
certid[1] = 300
certid[2] = 300
certid[3] =
0

Please read my answer again and you will see I changed the values in the noc1 fields for demonstration purposes.
It is expected that you will put the values back to what you need for your application.

It was not at all clear that you wanted unchecked rows set to zero. With the information I have given you that should not be too hard to figure out. There are a number of different ways to determine what inputs are missing from the $certid array.
Reply

#5
(04-16-2019, 09:02 AM)dave friend Wrote:
(04-16-2019, 07:13 AM)kvanaraj Wrote: I try your solution , but the output should be
certid[1] = 300
certid[2] = 300
certid[3] =
0

Please read my answer again and you will see I changed the values in the noc1 fields for demonstration purposes.
It is expected that you will put the values back to what you need for your application.

It was not at all clear that you wanted unchecked rows set to zero. With the information I have given you that should not be too hard to figure out. There are a number of different ways to determine what inputs are missing from the $certid array.
I attach complete my page source . i do know what mistake i ve done

Code:
<!-- list 1 -->


<?php foreach($getcert1 as $student1){        
       ?>
   <td width="50px" align="center">1</td>
   <td> Certificate 1</td>
   <td align="center">
     <input type = "checkbox" id="mycheck1" name="certid[]" class="checkbox"      
     value="1"  <?php echo set_checkbox($student1['certid'],'1',
     $student1['certid']==1);?> >
     </td>
   <td> 850 </td>
   

   <div class="col-xs-2">
   <td ><input type="text" id="totalamountremaining1"  name="txt[]" class="text-right" value = "<?php echo (!isset($student1['paid'])) ? 0 : $student1['paid'] ?>"  size="5"></td>
</div>  
 </tr>
<?php } ?>

<!-- list 2 -->

<?php foreach($getcert2 as $student2){
         ?>
   <td width="50px" align="center">2</td>
   <td>Certificate 2</td>
   <td align="center">
     <input type="checkbox" id="mycheck2" name="certid[]"  class="checkbox"      
     value="2"   <?php echo set_checkbox($student2['certid'],'1',
     $student2['certid']==2);?> >
     </td>
   <td> 300 </td>    
   
   <div class="col-xs-2">
   <td ><input type="text" id="totalamountremaining2"  name="txt[]" class="text-right" value="<?php echo (!isset($student2['paid'])) ? 0 : $student2['paid'] ?>"  size="5"></td>
</div>  
 </tr>
<?php } ?>


<!-- list 3 -->
 <?php foreach($getcert3 as $student3){  
       ?>
 <tr>
   <td width="50px" align="center">3</td>
   <td>Certificate 3</td>
   <td align="center"><input type="checkbox" id="mycheck3" name="certid[]" class="checkbox"  value="3" <?php echo set_checkbox($student3['certid'],'3',
     $student3['certid']==3);?>  > </td>
   <td> 300 </td>
   <td><input id="totalamountremaining3" type="text" name="txt[]" class="text-right"  value="<?php echo (!isset($student3['paid'])) ? 0 : $student3['paid'] ?>"  size="5"></td>
   
 </tr>
<?php } ?>
<!-- list 4 -->

<?php foreach($getcert4 as $student4){   ?>

 <tr>
   <td width="50px" align="center">4</td>
   <td>Certificate 4</td>
    <td align="center"><input type="checkbox" id="mycheck4" name="certid[]" value="4" class="checkbox"   <?php echo set_checkbox($student4['certid'],'1',
     $student4['certid']==4);?> > </td>
   <td> 300 </td>





   <td><input  id="totalamountremaining4" type="text" name="txt[]" class="text-right"  value="<?php echo (!isset($student4['paid'])) ? 0 : $student4['paid'] ?>" size="5"></td>
 
 </tr>
 <?php } ?>

<!-- list 5 -->

 <?php foreach($getcert5 as $student5){
       ?>
 <tr>
   <td width="50px" align="center">5</td>
   <td>Certificate 5</td>
   <td align="center"><input type="checkbox" id="mycheck5" name="certid[]" class="checkbox" value="5"  <?php echo set_checkbox($student5['certid'],'1',
     $student5['certid']==5);?> > </td>
   <td> 300 </td>
   <td><input id="totalamountremaining5" type="text" name="txt[]" class="text-right"  value="<?php echo (!isset($student5['paid'])) ? 0 : $student5['paid'] ?>" size="5"></td>
   
 </tr>
 <?php } ?>

 <!-- list 6 -->
 <?php foreach($getcert6 as $student6){
       ?>
 <tr>
   <td width="50px" align="center">6</td>
   <td>Certificate 6</td>
   <td align="center">

     <input type="checkbox" id="mycheck6" name="certid[]" value="6" class="checkbox" <?php echo set_checkbox($student6['certid'],'1',
     $student6['certid']==6);?> > </td>
   <td> 300 </td>
   <td><input  id="totalamountremaining6" type="text" name="txt[]" class="text-right"  value="<?php echo (!isset($student6['paid'])) ? 0 : $student6['paid'] ?>" size="5"></td>
   
 </tr>
 <?php } ?>

 <!-- list 7 -->
 <?php foreach($getcert7 as $student7){
       ?>
 <tr>
   <td width="50px" align="center">7</td>
   <td>Certificate 7</td>

   <td align="center"><input type="checkbox" id="mycheck7" name="certid[]" value="7" class="checkbox"  <?php echo set_checkbox($student7['certid'],'1',
     $student7['certid']==7);?> > </td>
   <td> 300 </td>
 


   <td><input  id="totalamountremaining7" type="text" name="txt[]" class="text-right"  value="<?php echo (!isset($student7['paid'])) ? 0 : $student7['paid'] ?>"  size="5"></td>
   
 </tr>
 <?php } ?>

 <tr>
   
   <td></td>
   <td><b><span id="sum1"></span></b></td>
    <td></td>
   
    <td><span><b>TOTAL</b></span></td>
    <td><input id="sum" type="text" name="sum" value="<?php echo (!isset($sum)) ? 0 : $sum ?>" class="text-right" size="5" ></td>
</tr>
Reply

#6
I think your not understanding how to use the onclick event.

Onclick Event
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#7
(04-20-2019, 04:14 AM)InsiteFX Wrote: I think your not understanding how to use the onclick event.

Onclick Event

I am not asking onclick related query. array of insertion value some time missing.
Reply

#8
Quote:I attach complete my page source . i do know what mistake i ve done
So your problem is solved now?
Reply

#9
@kvanaraj, Please show the controller method that loads the complete page source.
Reply

#10
(04-20-2019, 03:00 PM)dave friend Wrote: @kvanaraj, Please show the controller method that loads the complete page source.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.