2016-12-19 114 views
1

建模我需要创建经由AJAX填充数据与下面的代码的表:值传递给从控制器

VIEW:

<form class="form-inline" id="form_filter" method="POST"> 
          <label for="date_from">From </label> 
          <input type="date" name="date_from"> 

          <label for="date_to">&nbsp;&nbsp;To </label> 
          <input type="date" name="date_to"> 

          <input type="button" name="btn_view_records" value="Filter Date" class="btn btn-success btn-sm" id="btn_view_records"> 
         </form> 

$('#btn_view_records').click(function(){ 
     $.ajax({ 
      type:"POST", 
      data:$('#form_filter').serialize(), 
      datataype:"JSON", 
      url:"<?php echo site_url('pakyaw/get_filtered_data');?>", 
      success:function(data){ 
       $("#log-list").html(data); 
      } 
     }); 
    }); 

CONTROLLER:

public function get_filtered_data(){ 
     $this->load->library('table'); 

     $this->table->set_heading('Bio Id', 'Log Date', 'Time In', 'Time Out', 'Time Rendendered'); 
     $style = array('table_open' => '<table class="table table-striped table-hover">'); 
     $this->table->set_template($style); 
     echo $this->table->generate($this->model_pakyaw->get_filtered_data($this->session->userdata('branch_name'), $this->input->post('date_from'), $this->input->post('date_to'))); 
    } 

型号:

public function get_filtered_data($branch_name, $date_from, $date_to){ 
     $this->output->enable_profiler(TRUE); 

     $str2="select bio_id as 'bio_id', cast(attendance as date) as 'Log Date', min(attendance) as 'time_in', max(attendance) as 'time_out', timediff(max(attendance), min(attendance)) as 'difference' 
      FROM delwater_downydb.pakyaw_attendance 
      WHERE attendance > '".$date_from."' 
      GROUP BY bio_id, cast(attendance as date) 
      ORDER BY bio_id, cast(attendance as date) desc;"; 
     $query=$this->db->query($str2); 
     return $query->result_array(); 
    } 

当我点击btn_view_records时,它不会过滤结果。我查了一下,发现date_from没有被传递。 SQL语法越来越''值,这就是为什么它没有正确过滤。你能告诉我我做错了什么吗?谢谢。

+0

该代码看起来没有错。你可以显示'var_dump($ _ POST)'? – weirdo

+0

我该如何实现?对不起,基本上是新的PHP .. – Ibanez1408

+0

当我这样做“print_r($ _ POST);死();”这只是这个.....“Array()” – Ibanez1408

回答

0

在您的ajax调用中输入错字?

datataype:"JSON",应分配后的值或者尝试在模型打印POST值dataType:"JSON",

0

传递变量。

print_r($_POST); //in model 

$from_date = $this->input->post('date_from');//in model 
$date_to= $this->input->post('date_to');//in model 

$result = $this->table->generate($this->model_pakyaw->get_filtered_data($from_date, $date_to, $any_other_variable);// in controller 

print_r($result);// in controller