2016-10-21 2 views
0

Ich habe ein Formular, Abfrageergebnis zu filtern. Es ist nur Filter basierend auf dem Datum. Irgendwie wird mein Eingabewert nicht an den Controller gesendet, aber wenn ich ihn logge, wird der Wert wie erwartet angezeigt.Eingabewert von Ajax nicht an den Controller gesendet

Meine Ansicht:

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

Mein Controller:

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'); 
} 

Es gibt mir alle Zeilen Eingabe ignoriert email, input_range_to und input_range_from. FYI, ich benutze jQuery daterangepicker. Was habe ich falsch gemacht?

+0

setzen dieses 'echo $ this-> db-> last_query() 'return;' unter dieser Zeile '$ query = $ this-> db-> get(); 'in Ihrem Controller und dann die Abfrage hier (Sie können es in der Konsole finden) –

Antwort

0

sollten Sie das Standardverhalten des Click-Ereignis der Schaltfläche vermeiden

  1. Sie sollten return false in JavaScript-Funktion promotion wie folgt:

    <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. Sie eine return vor der Beförderung Funktion hinzufügen sollten rufen Sie wie folgt an:

    <button type="button" class="btn btn-primary" onclick="return promotion()">View</button> 
    

OR: Sie eine andere Lösung wie folgt versuchen:

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

Vielen Dank für Ihre Antwort, ich habe meine Frage bearbeitet und tun, wie Sie vorgeschlagen, aber ich bekomme immer noch die gleiches Ergebnis. Irgendwie, der Wert von Input-Post nicht an den Controller gesendet, ignoriert es die "E-Mail" und Datumsbereich :( – may

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

Vielen Dank für Ihre Antwort Haben Sie eine bessere Ansatz zeigt das Ergebnis auf andere Seite nach dem Senden des Formulars? Ich benutze 'window.location.href' :( – may

Verwandte Themen