I have two checkboxes named as inspection_element_id and datatype id. both checkbox values are come from database because they are dynamic. when I click on one inspection_element_id, all checboxes of datatype should open and I check some of them. then I check second checkbox of inspection_element_id, all checkboxes of datatype should open respectively and I check checkboxes of datatype. I want to store data in database in codeigniter like below:
Tbl_inspection:
|Inspection Element ID| | Datatype ID |
| (Parent Checkbox) | |(Child Checkbox)|
|_____________________| |________________|
| 4 | | 3 |
| 4 | | 5 |
| 6 | | 8 |
| 6 | | 12 |
In the table above, when I check 4th checkbox of inspection element, and from datatype I check 3rd and 5th checkbox. after that, I check 6th checkbox of inspection_element_id and from datatype checkboxes, I check 8th and 12th checkbox, it should be store in database like this. Please help me to solve this.
please check my code below:
View:
<?php echo form_open_multipart('inspection/add', array("class" => "form-horizontal")); ?>
<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>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-3 no-padding-right" for="datatype">Select Datatype: </label><br>
<div class="col-xs-12 col-sm-9">
<div class="clearfix">
<div id="dvDatatype">
</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+=' <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())
{
$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_id($params_checkbox1)
{
$this->db->insert('tbl_inspection',$params_checkbox);
}
Aucun commentaire:
Enregistrer un commentaire