2017-08-19 86 views
0

我的臉,而編碼一個問題,事情是,我想從發送腳本到另一個檢索相同的數據讓我解釋一下不斷通過AJAX請求檢索在PHP中發送數據

<button type="button" name="btn_more" data-vid="<?php echo $product; ?>" 
id="btn_more">Load more data</button> 
<input type="hidden" name="category" value="<?=$category;?>" id="category"> 

我有到按鈕首先調用Ajax和傳遞參數,第二個擁有數據庫中的數據

$(document).ready(function(){ 
    $(document).on('click', '#btn_more', function(){ 
     var last_product_id = $(this).data("vid"); //this stands for <button> 
     var cat=$("#cat").val(); //this one for hidden input 
     $('#btn_more').html("Loading..."); 
     $.ajax({ 
      url:"ajax/shopProduct.php", 
      method:"POST", 
      data:{last_product_id:last_product_id, category:category}, 
      dataType:"text", 
      success:function(data) { 
       if(data != 'No rows') { 
        $('#remove_row').remove(); 
        $('#load_data_table').append(data); 
       } else { 
        $('#btn_more').html("No results"); 
       } 
      } 
     }); 
    }); 
}); 
在shopProduct.php

,在這裏我得到這個職位數據

if($_POST) { 
    $let = filter_var($_POST["last_product_id"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); 
    $category= filter_var($_POST["category"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); 
} 

而基於這兩個值我做一個SQL語句來獲取數據,並顯示一些信息的輸出,並再次顯示負載更多按鈕

<div id="remove_row"><button type="button" name="btn_more" data-vid="<?php 
echo $product; ?>" id="btn_more">Load more data</button></div> 

實際的問題是,$category值停止作爲第二次點擊來自其他腳本的調用。我如何連續從第一個腳本獲取$category值到當前值?

回答

1

您正在使用錯誤的選擇器。改變這一行 -

var cat =$("#cat").val() 

var category = $("#category").val() 
+0

第一次調用工作,因爲它是發送$類值,從另一個腳本按鈕過程的第二個電話,並且對$類沒有更多的價值 變量變爲空 – Andrew

+0

但是,您隱藏的輸入元素具有id = category.How即使第一次您可以獲得它的值.. !!! –

+0

value =「」保存來自數據庫的信息 – Andrew