【问题标题】:Autosuggest from php来自 php 的自动建议
【发布时间】:2011-11-04 16:23:02
【问题描述】:

我有一个 php 文件,它从我的 mysqldb 中读取数据作为 json 数组。 如果用户输入 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”中将数据放入数组中。

【问题讨论】:

  • 浏览器接收到的页面的 HTML 源代码是什么?

标签: php javascript html json


【解决方案1】:
var availableTags = JSON.parse(<?php include("/php/getAllRecipes.php"); ?>);

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

【讨论】:

  • 你为什么需要解析它,它不只是一个对象的普通内联声明吗?
猜你喜欢
  • 2011-12-28
  • 2012-05-18
  • 2011-10-10
  • 1970-01-01
  • 2011-04-08
  • 1970-01-01
  • 2012-03-17
  • 1970-01-01
  • 2016-09-25
相关资源
最近更新 更多