使用 JS 和 PHP 从数据库中获取信息


getting info from a database with js and php

我真的希望有人能指出我正确的方向,因为我对js相当陌生。

我有一个下拉导航菜单,内容填充如下。

var anylinkmenu1={divclass:'anylinkmenu', inlinestyle:'', linktarget:''} 
anylinkmenu1.items=[
["Page 2 Subpage 1", "http://kellys-webdesigns.com/mpanetwork/index.php? id=8"],
["Page 2 Subpage 2", "http://kellys-webdesigns.com/mpanetwork/index.php? id=9"],
["Page 2 Subpage 3", "http://kellys-webdesigns.com/mpanetwork/index.php? id=10"],
["Page 2 Subpage 4", "http://kellys-webdesigns.com/mpanetwork/index.php? id=11"],
["Page 2 Subpage 5", "http://kellys-webdesigns.com/mpanetwork/index.php? id=12"],
["Page 2 Subpage 6", "http://kellys-webdesigns.com/mpanetwork/index.php? id=13"],
["Page 2 Subpage 7", "http://kellys-webdesigns.com/mpanetwork/index.php? id=14"],
["Page 2 Subpage 8", "http://kellys-webdesigns.com/mpanetwork/index.php? id=15"] 
]
var anylinkmenu2={divclass:'anylinkmenu', inlinestyle:'', linktarget:''} 
anylinkmenu2.items=[
["Page 3 Subpage 1", "http://kellys-webdesigns.com/mpanetwork/index.php? id=16"],
["Page 3 Subpage 2", "http://kellys-webdesigns.com/mpanetwork/index.php? id=17"],
["Page 3 Subpage 3", "http://kellys-webdesigns.com/mpanetwork/index.php? id=18"],
["Page 3 Subpage 4", "http://kellys-webdesigns.com/mpanetwork/index.php? id=19"],
["Page 3 Subpage 5", "http://kellys-webdesigns.com/mpanetwork/index.php? id=20"],
["Page 3 Subpage 6", "http://kellys-webdesigns.com/mpanetwork/index.php? id=21"],
["Page 3 Subpage 7", "http://kellys-webdesigns.com/mpanetwork/index.php? id=22"],
["Page 3 Subpage 8", "http://kellys-webdesigns.com/mpanetwork/index.php? id=23"] 
]

内容是数据库的一部分,我希望它们从数据库中获取,而不是手动输入这些值,这可能吗?我想我需要使用 php 来实现这一点,我对 php 代码没有问题,我只是不确定如何将 js 连接到 php。

任何帮助将不胜感激。

进行ajax调用,获取数据并将其分配给variables

使用jquery library或类似library

PHP可以编写javascript和HTML。

以下脚本假定$menuItems是数据库中的"title"和"url"数组,$totalItems是$menuItems中的项目数。

$totalItems是必需的,因为否则 php 会在最后一项的末尾生成一个额外的","(逗号),这使得 IE 适合。

<script type="text/javascript">
    var anylinkmenu1={divclass:'anylinkmenu', inlinestyle:'', linktarget:''} 
    anylinkmenu1.items=[
    <?php $index=0;
        foreach ($menuItems as $item): ?>
        ["<?php echo $item["title"]; ?>", "<?php echo $item["url"] ?>"] <?php if ($index-1 > $totalItems) { echo ","; } ?>
    <?php $index += 1; endforeach; ?>
    ]
</script>