我正在用 PHP、Javascript 和 MySQL 创建一个下拉菜单。我通过 Javascript/AJAX 成功地与另一个 PHP 文件通信。在 javascript 中,我发送一个带有我在 PHP 文件中捕获的 URL 的 ID。在 PHP 文件中,我想检查 ID 的值,然后用它来检查它应该执行什么查询。然后我想在每次翻转时打印这些值。我想我快到了,但我现在很震惊。
Javascript:
function onRollOver(id)
{
if (window.XMLHttpRequest)
{
test=new XMLHttpRequest();
}
else
{
test=new ActiveXObject("Microsoft.XMLHTTP");
}
// document.getElementById('test').style.display="block";
if (id == 1)
{
console.log("This is one");
}
else if (id == 2)
{
console.log("this i stwo");
}
console.log("id = " + id);
test.open("GET","get_menu_id.php?id="+id,true);
test.send();
}
function onRollOut()
{
document.getElementById('test').style.display="none";
}
PHP 和 HTML:
while($names = $get_category_names->fetch_object())
{
$li_data = "<li data-dir=";
//echo $li_data . $names->id . ">" . $names->name . "</li>";
echo "<a onmouseover='onRollOver($names->id);' onmouseout='onRollOut($names->id);' >" . "$names->name" . "</a>";
}
外部 PHP 文件:
<?php
require_once("../includes/constants.php");
require_once("../includes/connection.php");
require_once("functions.php");
$connect = connectDB();
//get the q parameter from URL
$id=$_GET["id"];
//lookup all hints from array if length of q>0
echo $id;
if ($id == 1)
{
$nintendo = $connect->query("SELECT DISTINCT (categorypath) FROM feeds WHERE category_id = 1");
while($names = $nintendo->fetch_object())
{
echo "<p>" . $names->categorypath . "</p>";
}
}
?>