来自ajax的输入值未发送到控制器

时间:2016-10-21 08:32:37

标签: jquery ajax codeigniter daterangepicker

我有一个表单来过滤查询结果。它只是根据日期过滤。不知何故,我的输入值没有发送到控制器,但是当我控制台登录时,它会显示我预期的值。

我的观点:

<form action="" method="post" id="cashback">
User Email :
<input type="text" name="email" id="email">
<br><br>
Booking Date :
<div id="reportrange" style="background: #fff; cursor: pointer; padding: 5px 10px; border: 1px solid #ccc; display:inline">
    <span></span> <b class="caret"></b>
</div>
<input type="hidden" name="input_date_from" id="input_date_from" value="">
<input type="hidden" name="input_date_to" id="input_date_to" value=""><br><br>
<button type="button" class="btn btn-primary" onclick="promotion()">View</button>
<br><br>
</form>
<script>
function promotion() {

email = $('#email').val();
input_date_from = $('#input_date_from').val();
input_date_to = $('#input_date_to').val();

$.ajax
    ({
        url : "<?php echo site_url('admin/check_promotion')?>",
        type: "POST",
        dataType: "text",
        data:{email: email, input_date_to: input_date_to, input_date_from: input_date_from},
        success: function(data)
        {
            console.log(email);
            console.log(input_date_from);
            console.log(input_date_to);
            window.location.href = "<?php echo site_url('admin/check_promotion')?>";
        }
    });
}
</script>

我的控制器:

public function check_promotion()
{
    if(!$this->user_permission->check_permission())return;

        $data['startDate'] = $this->input->post('input_date_from');
        $data['endDate'] = $this->input->post('input_date_to');
        $email = $this->input->post('email');

        $this->db->select('a.booking_id as booking_id, a.from_email as from_email, a.booking_date as booking_date, a.status as status, b.vendor_airway_bill as awb, b.tariff as tariff');
        $this->db->from('booking as a');
        $this->db->join('shipment as b','a.booking_id = b.booking_id','left');
        $this->db->where('booking_date >=', date('Y-m-d',strtotime($data['startDate'])));
        $this->db->where('booking_date <=', date('Y-m-d',strtotime($data['endDate'])));
        $this->db->where('from_email', $email);
        $this->db->where('status = 1');
        $this->db->or_where('status = 2');
        $this->db->limit('300');
        $query = $this->db->get();
        $data['result'] = $query->result_array();

        $this->template->render('admin/promotion',$data,'admin');
}

它为我提供了忽略输入emailinput_range_toinput_range_from的所有行。仅供参考,我正在使用jQuery daterangepicker。我做错了什么?

2 个答案:

答案 0 :(得分:0)

你应该避免按钮的点击事件的默认行为

  1. 您应该在javascript函数return falsepromotion,如下所示:

    <script>
    function promotion() {
    
      email = $('#email').val();
      input_date_from = $('#input_date_from').val();
      input_date_to = $('#input_date_to').val();
    
      $.ajax
      ({
        url : "<?php echo site_url('admin/check_promotion')?>",
        type: "POST",
        dataType: "text",
        data:{email: email, input_date_to: input_date_to, input_date_from: input_date_from},
        success: function(data){
          console.log(email);
          console.log(input_date_from);
          console.log(input_date_to);
          window.location.href = "<?php echo site_url('admin/check_promotion')?>";
        }
      });
      return false;
    }
    </script>
    
  2. 您应该在促销函数调用之前添加return,如下所示:

    <button type="button" class="btn btn-primary" onclick="return promotion()">View</button>
    
  3. 或者:您可以尝试以下其他解决方案:

    <form action="" method="post" id="cashback">
    User Email :
    <input type="text" name="email" id="email">
    <br><br>
    Booking Date :
    <div id="reportrange" style="background: #fff; cursor: pointer; padding: 5px 10px; border: 1px solid #ccc; display:inline">
      <span></span> <b class="caret"></b>
    </div>
    <input type="hidden" name="input_date_from" id="input_date_from" value="">
    <input type="hidden" name="input_date_to" id="input_date_to" value=""><br><br>
    <button type="button" id="btn-submit" class="btn btn-primary">View</button>
    <br><br>
    </form>
    <script>
    $('#btn-submit').click(function(e){
      e.preventDefault();
    
      email = $('#email').val();
      input_date_from = $('#input_date_from').val();
      input_date_to = $('#input_date_to').val();
    
      $.ajax({
        url : "<?php echo site_url('admin/check_promotion')?>",
        type: "POST",
        dataType: "text",
        data:{email: email, input_date_to: input_date_to, input_date_from: input_date_from},
        success: function(data)
        {
          console.log(email);
          console.log(input_date_from);
          console.log(input_date_to);
          window.location.href = "<?php echo site_url('admin/check_promotion')?>";
        }
      });
    });
    </script>
    

答案 1 :(得分:0)

Following code is useful to send the data from view to controller using ajax.

  <script>
    function promotion() {
      $.ajax
      ({
        url : "<?php echo site_url('admin/check_promotion')?>",
        type: "POST",
        data:$('#cashback').serialize(), //$('#cashback') is the form id
        success: function(){
            location.reload(); // This will refresh the page after submit or you can do whatever you want.
        }
      });
    }
    </script>

    Now, You Can get the Posted date in your controller or model using the input field name like

    $email              =   $this->input->post('email');
    $input_date_from    =   $this->input->post('input_date_from');
    $input_date_to      =   $this->input->post('input_date_to');

    Thanks,