我有一个php文件,它以json数组的形式从mysqldb中读取数据。
如果用户键入3个字符,则应显示数据。

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”中将数据放入数组。

最佳答案

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


假设您的PHP文件输出有效的JSON,并带有引号。
如:echo '"'.addslashes(json_encode($data)).'"';

07-28 02:47
查看更多