1
$('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.

KUMAR
  • 1,993
  • 2
  • 9
  • 26
Renuga G
  • 11
  • 2
  • And which line is 133, please? Remember we can't see your IDE. – ADyson Mar 28 '22 at 08:30
  • P.S. Something doesn't make sense here overall. You're handling the "change" event on the "empName" element twice. "empName" suggests it means "employee", perhaps? Yet you then take the selected value from that element, and send it to two different AJAX endpoints, with two different names "quan" and "dept" (neither of which seem obviously to be an employee name or ID!), which then cause the server to attempt to fetch two separate pieces of data using that same ID... – ADyson Mar 28 '22 at 08:34
  • ... It surely cannot be the case that the same ID is both a department ID _and_ an employee ID? Did you set one of the "change" events on the wrong element, perhaps? We can't see your HTML or guess your precise intentions from the vague description, so it's hard to be sure, but it seems like you've probably just made a silly mistake somewhere. – ADyson Mar 28 '22 at 08:35
  • 1
    P.P.S. Your PHP code should probably also check whether any employee or department is actually returned from the queries before attempting to echo items from that data! That way if an invalid ID is ever used, the code won't crash or throw out warnings. – ADyson Mar 28 '22 at 08:36
  • hello , you want on select(change) of 1 drop down , get 2 input values by ajax , is it correct??? – KUMAR Mar 28 '22 at 16:46
  • that line 133 was echo $value['department']; – Renuga G Mar 29 '22 at 15:20
  • That'll be because `$value` is null. That's likely because the value you submitted doesn't match any departments, so the query returned nothing. And that's likely to be due the logic error I've outlined in my earlier comments. – ADyson Mar 29 '22 at 15:30
  • can you please tell me the exact code ? – Renuga G Mar 31 '22 at 05:03
  • No, because as I also already mentioned, it's unclear what precisely you were trying to do instead. You need to explain the exact requirement better before we can give a definite answer. – ADyson Mar 31 '22 at 08:18

1 Answers1

-1

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 :-

<select name="test" id="test" >
<option value="user1">user1</option>
<option value="user2">user2</option>
<option value="user3">user3</option>
<option value="user4">user4</option>
<option value="user5">user5</option>
</select>
<input type="text" placeholder="Address" id="address"/>
<input type="text" placeholder="Name" id="name"/>
<input type="text" placeholder="Contact" id="contact"/>

JQuery / Ajax Part:-

<script type="text/javascript">
    $('#test').change(function(){
    var test=$(this).val();
    $.ajax({
        type: "GET",
            url: '<?php echo base_url("index.php/emp_salary/ajaxReq"); ?>',
            data: {test:test},
            success:function(data)
            {
            var result = $.parseJSON(data);
            $("#address").val(result.address);
            $("#name").val(result.name);
           $("#contact").val(result.contact);     
            }
         })
    }); 
    </script>

controller part:-

public function ajaxReq() {
  $test = $this->input->post('test');
  $value = $this->salary_model->getVal($test);
  echo json_encode($value);
 }

Model part :-

public function getVal($test){
    $this->db->select('*');
    $this->db->from('employee');
    $where = array('empId' => $test );
    $this->db->where($where);
    $query = $this->db->get();
    return $query->row_array();
}
KUMAR
  • 1,993
  • 2
  • 9
  • 26