【问题标题】:input value from ajax not sent to controller来自ajax的输入值未发送到控制器
【发布时间】:2016-10-21 08:32:37
【问题描述】:

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

我的看法:

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

【问题讨论】:

  • 把这个echo $this-&gt;db-&gt;last_query()'return;放在你的控制器中$query = $this-&gt;db-&gt;get();这行下面,然后在这里显示查询(你可以在控制台中找到它)

标签: jquery ajax codeigniter daterangepicker


【解决方案1】:

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

  1. 你应该在javascript函数promotionreturn false如下:

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

【讨论】:

  • 感谢您的回复,我编辑了我的问题并按照您的建议进行操作,但我仍然得到相同的结果。不知何故,输入帖子的值没有发送到控制器,它忽略了email 和日期范围:(
【解决方案2】:
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,

【讨论】:

  • 感谢您的回复。提交表单后,您有没有更好的方法将结果显示到其他页面?因为这就是我使用window.location.href :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-28
  • 1970-01-01
  • 2022-11-05
  • 2019-10-21
  • 2020-11-19
  • 2020-11-14
  • 1970-01-01
相关资源
最近更新 更多