2016-01-18 91 views
0

我有一個搜索表單,我想通過jQuery自動完成顯示建議,當輸入3個字符時。這些建議來自mySQL DB。 發生了什麼:jQuery確實將類型化的字符成功傳輸到PHP文件,併成功嵌入到mySQL查詢中。 當我用假定的搜索詞分開打開PHP文件時,f.e.像這樣:/soplogsearch.php?term=xyz它完美的作品,我看到的目標是echo json_encode($return_arr); 的結果,但回到HTML搜索表單文件自動完成不提示的東西。我在控制檯中沒有錯誤。我試圖在HTML文件的其他地方回顯json_encode,它的輸出是NullPHP數組作爲JSON響應jQuery自動完成

我已經做了小提琴的(很簡單)的Javascript/jQuery和HTML設置:https://jsfiddle.net/9bf6s07f/1/

相關的PHP代碼如下所示:

if (isset($_GET['term'])) 
    { 
    $term = $_GET['term']; 
    $termwild = "%$term%"; 
    $return_arr = array(); 
    $service = mysql_query("SELECT DISTINCT service FROM master WHERE service LIKE \"" . $termwild . "\""); 
    while ($data = mysql_fetch_array($service)) 
     { 
     $return_arr[] = $data[0]; 
     } 
    json_encode($return_arr); 
    echo json_encode($return_arr); 
    } 

編輯:爲了更快地訪問我米包括代碼的HTML和jQuery零件,而不是在這裏的鏈接到你的小提琴https://jsfiddle.net/9bf6s07f

<body> 
    <label>Service:</label> 
    <input type='text' name='' value='' class='auto'> 
</body> 

和jQuery:

$(document).ready(function() { 
    $(function() { 
    $(".auto").autocomplete({ 
     source: "soplogsave.php", 
     minLength: 3 
    }); 
    }); 
}); 

有人知道我在做什麼錯嗎?我用一組javascript變量測試了自動完成,並且它工作正常。

編輯2:因爲所有的評論似乎意味着我的PHP是錯誤的,我有一個錯誤在控制檯中,我從控制檯的網絡選項卡截圖:http://i.imgur.com/i6nAQ98.png

+0

我不認爲你的查詢是正確的。試試這個: '$ service = mysql_query(「SELECT DISTINCT服務FROM主WHERE服務LIKE'%{$ term}%'」);' 我正在做一個類似的事情,在我的應用程序中,就像你試圖做的一樣。 – ArtleMaks

+0

你的PHP文件是否返回結果? – HJerem

+0

但這個查詢確實有效! mysql_fetch_array – sardine

回答

0

這是怎麼了我在我的代碼來實現它:

PHP

$param = $_GET["term"]; 

$stk_adddep = " 
SELECT * FROM stocktake_products WHERE stocktake_id = '{$stocktake_id}' AND is_deli = 0 AND (product_code LIKE '%{$param}%' OR product_name LIKE '%{$param}%'); "; 

//FB::info($stk_adddep); 
//echo $stk_adddep; 
//die(); 
$result = db::c()->query($stk_adddep); 
while ($row = $result->fetch(PDO::FETCH_ASSOC)) { 

    $row_array['itemCode'] = $row['product_code']; 
    $row_array['itemDesc'] = $row['product_name']; 
    //$row_array['itemPrice'] = $row['unit_cost_price']; 

    array_push($return_arr, $row_array); 
} 

/* Free connection resources. */ 
//mysql_close($conn); 

/* Toss back results as json encoded array. */ 
echo json_encode($return_arr); 

然後JavaScript的

$(document).ready(function(){ 
// Use the .autocomplete() method to compile the list based on input from user 
var url10 = '<?php echo Navigation::gUrl('/users/admin/stocktake_details_cocktails.php', array('stocktake_id' => $stocktake_id, 'action' => 'find_products'));?>'; 

$('#itemCode').autocomplete({ 
    source: url10, 
    minLength: 1, 
    select: function(event, ui) { 
     var $itemrow = $(this).closest('tr'); 
       // Populate the input fields from the returned values 
       $itemrow.find('#itemCode').val(ui.item.itemCode); 
       $itemrow.find('#itemDesc').val(ui.item.itemDesc); 
       //$itemrow.find('#itemPrice').val(ui.item.itemPrice); 

       // Give focus to the next input field to recieve input from user 
       $('#itemQty').focus(); 

     return false; 
    } 
// Format the list menu output of the autocomplete 
}).data("autocomplete")._renderItem = function(ul, item) { 
    return $("<li></li>") 
     .data("item.autocomplete", item) 
     .append("<a>" + item.itemCode + " - " + item.itemDesc + "</a>") 
     .appendTo(ul); 
}; 

看看你是否可以將它應用到你的代碼?

+0

謝謝,但這是太廣泛,我的簡單代碼:(我不是在JavaScript中識字 – sardine

0

答案發布在用戶@ n00dl3的評論中。