2011-11-04 73 views
0

我有一個PHP文件,從我的mysqldb讀取數據作爲json數組。 如果用戶輸入3個字符,則應顯示數據。從php的Autosuggest

JS:

<link rel="stylesheet" href="js/demos.css"> //JQuery AutoSuggest Css 
    <script> 
    $(function() { 
     var availableTags = <?php include("/php/getAllRecipes.php"); ?>; 


     $("#searchrecipes").autocomplete({ 
      source: availableTags 
     }); 
    }); 
    </script> 
     <input id="searchrecipes" type="text" name="searchrecipes" class="searchinput" style="margin-left: 850px; margin-top: 0px; width:170px; background: #fff url(images/search_icon.png) no-repeat 100%;" placeholder="Suchen..."></input> 
     <input type="submit"1 name="buttonsenden" style="display:none;" value="" width: 5px></input> 

問題:

我們必須讓我們的JS卡列斯 「availableTags」 數據放入數組。

+1

頁面的HTML源代碼是什麼,因爲它是由您的瀏覽器接收的? – CodeCaster

回答

4
var availableTags = JSON.parse(<?php include("/php/getAllRecipes.php"); ?>); 

這是假設你的PHP文件輸出有效JSON,WITH包圍引號。
例如:echo '"'.addslashes(json_encode($data)).'"';

+0

爲什麼你需要解析它,它不僅僅是一個對象的正常內聯聲明嗎? – alinn