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

input value from ajax not sent to controller

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

我的看法:

<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。我做错了什么?

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

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

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

或者:您可以尝试另一种解决方案,如下所示:

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