2014-02-05 48 views
0

我使用php從html加載輸入字段時出現了一些問題。我正在使用jQuery Ajax $.post函數將我的數據從輸入字段發送到php文件以便從數據庫執行查詢。Jquery.ajax使用php從html加載數據使用php加載數據

我修復了代碼。下面的代碼完美地工作。

這裏是我的主網頁

<input class="detail" maxlength="60" name="detail" placeholder="Type to search"> 
<button class="searchbtn" name="search">Search</button> 

的HTML,這是jQuery的阿賈克斯部分

$(".searchbtn").click(function(e) { 
    makeAjaxRequest(); 
}); 

function makeAjaxRequest(){ 
var input = $(".detail").val(); 
$.post("search.php",{detail:input},function(data,status){ 
     $('#resultTable tbody').html(data); 

}); 

然後最後一部分是PHP文件(的search.php)

if (isset($_POST['detail'])) { 
    $data = $_POST['detail']; 

    $query = "SELECT * FROM products WHERE ".%data; 
$result = $conn->query($query) or trigger_error($mysqli->error."[$sql]"); 

while($row = $result->fetch_assoc()){ 
    $rows[] = $row; 
} 
} 

現在它運作完美。感謝你的回答。

回答

0

您在您的服務器端網頁上提取$_POST['detail'],但在發送的客戶端,您發送這樣的,$.post("search.php",input

數據應該作爲PlainObject或字符串發送數據。我的意思是,每個POST數據都應該有一些索引來獲取另一端的引用。

既然你與指數detail提取,對下面的像添加detail

$.post("search.php",{detail:input},function(data,status){ 

參見:jQuery : $.post()

0

目前你只是發送一個沒有鍵/值的字符串。您需要發送一個鍵/值對您的數據參數,無論是作爲一個字符串或對象(對象是安全的,因爲it'l得到自動url編碼)

$.post("search.php",{detail:input},function(data,status){ 
0

你可以試試這個。

function makeAjaxRequest(){ 
    var input = $(".detail").val(); 
    $.post("search.php", 
     {detail: input}, 
     function(data,status){ 
      $('#resultTable tbody').html(data); 

    }); 
}