mardi 22 décembre 2020

If two parent checkbox checked and i check child checkbox values respectively then id of both checkbox should be store in database in Codeigniter

enter image description here

If two parent checkboxes checked and I checked their child checkbox values respectively then id of both parent and child checkbox should be store in database. This code is storing same data two times. But I want to store checkboxes value one time. Thanks for consideration!!

View:

<div>
    <div class="form-group" >
        <label class="control-label col-xs-12 col-sm-3 no-padding-right"
            for="last_name">Assign Inspection:</label>
        <div class="col-xs-12 col-sm-9">
            <div class="clearfix">
                <?php foreach($aInspection as $a)
                {?> 
                    <input type="checkbox" name="inspection_element_id[]" class="checkbox"  value = "<?php echo $a->id;?>"  />
                    <label><?php echo $a->name;?></label>
                    
                <?php
                } 
                ?>

            </div>
        </div>
    </div>
</div>

jQuery / AJAX Code:

 <script type='text/javascript'>
    $(document).ready(function(){
    $(".checkbox").click(function(){
    if($(this).is(':checked'))
    {
    var value=$(".checkbox:checked").val();
    loadPagination(value);
    }
    else
    {
    console.log("NO");
    $('#dvDatatype').empty();
    }
    });
    function loadPagination(pagno){
    console.log('pagno',pagno)
    $.ajax({
    url: '<?=base_url()?>index.php/Inspection/show_inspection/'+pagno,
    type: 'get',
    dataType: 'json',
    data:{pagno:pagno},
    success: function(data){
    createTable(data);
    }
    });
    }
    function createTable(result)
    {
    console.log(result);
    for(index in result)
    {
    console.log(index); 
    var id = result[index].id;
    var ins_id = result['inspection_id'];
    var datatype=result[index].datatype_name;
    var input;
    input+= '<input type="hidden" name="inspection_id[]" value ="'+ins_id+'">';
    input+= '<input type="checkbox" name="datatype_id[]" value ="'+id+'">';
    input+='&nbsp;<label>'+datatype+'</label>';
    input+='<br>';    
    }
    $('#dvDatatype').append(input);
    }
    });
    </script>

Controller:

function show_inspection($pageno)
{
$data['datatype'] = $this->dtModel->get_all_datatype();
$data['inspection_id'] = $pageno;
echo json_encode($data['datatype']);
}
function add()
{   
    $data['designation'] = $this->dModel->get_all_designation();
    $data['aInspection'] = $this->iModel->get_all_inspection_element(); 
    $data['datatype'] = $this->dtModel->get_all_datatype(); 
    $this->load->library('form_validation');
    $this->form_validation->set_rules('designation_id','Designation ID','required');
    
    if($this->form_validation->run())     
    {   
        
        $params = array(
            'designation_id' => $this->input->post('designation_id'),
            'assigned_date'=>$this->input->post('assigned_date'),
            'completion_date'=>$this->input->post('completion_date'),               
        );
        
        $inspection_id = $this->Tbl_inspection_model->add_inspection($params);
        

        $checkbox = $this->input->post('inspection_element_id');
        $checkbox1 = $this->input->post('datatype_id');
        foreach ($checkbox as $a) 
        {
            foreach ($checkbox1 as $b)  
            {
            
            $params_checkbox1 = array(
                'inspection_element_id'=>$a,
                'datatype_id'=>$b,
                'frequency'=>$this->input->post('frequency'),
                'inspection_id'=>$inspection_id,
            );
            $this->Tbl_inspection_model->add_inspection_id($params_checkbox1);
            }
        }
        $this->session->set_flashdata('type', 'success');
        $this->session->set_flashdata('msg', 'Inspection Added Successfully!');
       redirect('inspection/index');
    
    }
    else
    {            
        $data['_view'] = 'inspection/add';
        $this->load->view('layouts/main',$data);
    }
}

Model:

function add_inspection_element($params)
{
    $this->db->insert('tbl_inspection_element',$params);
    return $this->db->insert_id();
}

Tbl_inspection:

|Inspection Element ID| |   Datatype ID  | 
|  (Parent Checkbox)  | |(Child Checkbox)|
|_____________________| |________________|
|         4           | |        3       |
|         4           | |        5       |
|         6           | |        8       |
|         6           | |        12      |



Aucun commentaire:

Enregistrer un commentaire