使用Javascript / AJAX和PHP打印出值

时间:2013-03-18 23:28:34

标签: php javascript html mysql ajax

我正在用PHP创建一个下拉菜单,Javascript和MySQL。我通过Javascript / AJAX成功地与另一个PHP文件通信。在javascript中,我发送的ID带有我在PHP文件中捕获的URL。在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>";
    }
}
?>

2 个答案:

答案 0 :(得分:0)

onRollover之后的test.send()函数... test.responseText将包含外部php文件的echo'd内容。我不确定你想用它们做什么。

答案 1 :(得分:0)