$('document').ready(function () {
$('.js-example-basic-single').select2();
$('#empName').on('change', function() {
var quan = $(this).val();
$.ajax({
url: '<?php echo base_url("index.php/emp_salary/ajaxReq"); ?>',
type: 'post',
data: {quan: quan},
})
.done(function(data) { //<---- we use .done here
$('#empId').val(data);
})
});
$('#empName').on('change', function() {
var dept = $(this).val();
$.ajax({
url: '<?php echo base_url("index.php/emp_salary/ajaxDept"); ?>',
type: 'post',
data: {dept: dept},
})
.done(function(data) {
$('#department').val(data);
})
});
});
controller part
public function ajaxReq() {
$quan = $this->input->post( 'quan' );
$value = $this->salary_model->getVal($quan);
echo $value['empId'];
}
public function ajaxDept() {
$dept = $this->input->post( 'dept' );
$value = $this->salary_model->getDept($dept);
echo $value['department'];
}
model part
public function getVal($quan){
$this->db->select('*');
$this->db->from('employee');
$where = array('empId' => $quan );
$this->db->where($where);
$query = $this->db->get();
return $query->row_array();
}
public function getDept($dept){
$this->db->select('*');
$this->db->from('employee');
$where = array('department' => $dept );
$this->db->where($where);
$query = $this->db->get();
return $query->row_array();
}
Error
A PHP Error was encountered
Severity: Warning
Message: Trying to access array offset on value of type null
Filename: controllers/Emp_salary.php
Line Number: 133
how I can solve this?
I expect that when the dropdown changes I get another input value automatically.
According to Your Question , what i Understand is you want, when the Select dropdown changes get 2 or more input value automatically by jQuery ajax in Codeigniter.
HTML Part :-
JQuery / Ajax Part:-
controller part:-
Model part :-