在Codeignitor中使用Ajax过滤表数据

时间:2018-09-26 05:17:40

标签: php ajax codeigniter

我无法使用Ajax过滤表数据。当我从下拉列表中选择BANK CREDIT时,它应以modeofpay(表列)作为“ BANK CREDIT”来获取员工详细信息,当我选择NEFT时,它应将modeofpay(表列)作为“ NEFT”来显示员工详细信息。截至现在,当我选择下拉菜单时,什么也没有发生。

控制器:

public function filter($key = '')
{
  $this->load->helper('url');

  if ( $key == 'BANK CREDIT' ) {
    $this->load->model('JcMeetingExpense_model');
    $data = $this->JcMeetingExpense_model->getCredit($key);
  } 
  else 
  {
    $this->load->model('JcMeetingExpense_model');
    $data = $this->JcMeetingExpense_model->getNeft($key);
  }
  echo json_encode($data);
}

型号:

public function getCredit($key)
{
  $sql = "SELECT * FROM employee WHERE modeofpay = '$key'";
  $data = $this->db->query($sql);
  return $data->result_array();   
}

public function getNeft($key)
{
  $sql = "SELECT * FROM employee WHERE modeofpay = '$key'";
  $data = $this->db->query($sql);
  return $data->result_array();   
}

查看:

<script type="text/javascript">
  var paymode = $("#mode").change(function(){
    $.ajax({
      type:"POST",
      url:url:'<?php echo base_url("JcMeetingExpense/filter/key/") ? 
               >'+paymode,
      data:"key="+paymode,
      dataType:'json',
      success:function(data){
        $("#viewjcexpense").html(data);
      },
      error:function(XMLHttpRequest){
        alert(XMLHttpRequest.responseText);
      }
    });
  });
</script>

<select name="mode" id="mode" >
  <option value="BANK CREDIT">CREDIT</option>
  <option value="NEFT">NEFT</option>
</select>

2 个答案:

答案 0 :(得分:0)

您的onchange活动正常吗? 让我们检查一下

$("#mode").change(function(){
alert(1);
});

如果您选择一个选项时将显示表示您的事件正在运行的警报,则

现在,如果一切正常,让我们尝试使用ajax并进行少量重新编码,这里我使用了post方法

[VIEW]

$("#mode").change(function(){
     $.ajax({
                type  : 'POST',
                url   : '<?=base_url(); ?>JcMeetingExpense/filter/',
                data  : { key : $("#mode").val() },
                success : function(data){
                       console.log(data);//let's check on console what's response is 
                    }
                });
 })

[CONTROLLER]

public function filter()
{
  $this->load->helper('url');


    $this->load->model('JcMeetingExpense_model');
    $dataKey = $this->JcMeetingExpense_model->get_data_by_key();


  echo json_encode($datadataKey);
}

[模型]

 public function get_data_by_key()
    {
     //do post here let's say
     $key = $this->input->post("key");
// i looked your query is vulnerable to SQL Injection 
/*      $sql = "SELECT * FROM employee WHERE modeofpay = '$key'"; 
      $data = $this->db->query($sql);
      return $data->result_array();   */
      //so let's use query builder 
      $this->db->select("*");
      $this->db->from("employee");
      $this->db->where("modeofpay",$key);
      $q = $this->db->get();
      return $q->result_array();
    }

现在在控制台中检查响应

答案 1 :(得分:0)

<script type="text/javascript">
  $("body").on('change','#mode',function(){
    $.ajax({
      type:"POST",
      url:url:'<?php echo base_url("JcMeetingExpense/filter/key/") ? 
               >'+paymode,
      data:"key="+paymode,
      dataType:'json',
      success:function(data){
        $("#viewjcexpense").html(data);
      },
      error:function(XMLHttpRequest){
        alert(XMLHttpRequest.responseText);
      }
    });
  });
</script>

尝试使用此脚本代替以前的脚本

我已将$("#mode").change(function(){});更改为$("body").on('change','#mode',function(){});