Codeigniter 中的 ajax jQuery 在下拉菜单更改时如何获取 2 个或更多输入值

发布于 2025-01-17 07:24:16 字数 1936 浏览 0 评论 0原文

$('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);
        })
    });
});

控制器部分

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'];
}

型号部分

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();
}

错误

遇到 PHP 错误

严重性:警告

消息:尝试访问 null 类型值的数组偏移量

文件名:controllers/Emp_salary.php

行号:133

我该如何解决此问题?

我希望当下拉列表更改时,我会自动获得另一个输入值。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罗罗贝儿 2025-01-24 07:24:16

根据您的问题,我理解的是您想要的,当选择下拉列表更改时,Codeigniter 中的 jQuery ajax 自动获取 2 个或更多输入值。

HTML 部分:-

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

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

控制器部分:-

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

模型部分:-

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();
}

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文