2016-12-19 112 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