0

我正在尝试从我的数据库中检索信息,并将其输出到我的页面。我能够对其进行硬核,因此要准确显示我想要做的事情位于此处:

我在 javascript 中没有任何控制台错误,但是一旦单击它,我就无法将数据附加到列表中。

HTML & JS:

<!doctype html><html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="shortcut icon" href="../img/sd.ico" />
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>  
</head>
<body>
<div id="body">
<div id="header"><h1><a href="#">Bookmarks</a></h1></div><!-- header -->
<iframe id="iFrame" name="iFrame"></iframe>
<div id="links"><ul id="accordion">

<li><div class="bookmarkTitle"><p><h4>CareerResources</h4></p></div></li><ul id="CareerResources">

</ul><li><div class="bookmarkTitle"><p><h4>CSS</h4></p></div></li><ul id="CSS">

</ul><li><div class="bookmarkTitle"><p><h4>JS</h4></p></div></li><ul id="JS">

</ul><li><div class="bookmarkTitle"><p><h4>JS: Jquery</h4></p></div></li><ul id="JS: jQuery">

</ul></div><!-- links end -->
</div><!-- body end -->
<script type="text/javascript">
$(document).ready(function(){       
    $("#accordion > li").click(function(e){
        var catName=$(e.target).text();
        $.getJSON("get.php?catName=" + catName, function(data){ 
            if (data.data){ 
                $.each(data.data,function(index,value){ 
                    var url = value.URL; 
                    var title = value.title; 
                    var desc = value.desc; 
                    alert(catName);
                    $("#"+catName).append('<a href="'+url+'" target="iFrame"><div class="bookmark"><p><h3>'+title+'</h3><br />'+desc+'</p></div></a>'); 
                }); 
            } 
        });
    }); 
}); 
</script>
</body>
</html>

PHP:

// retval: 0 - login ok, 1 - login failed, 2 - internal error
$json = array("retval" => 2, "data" => NULL, "debug" => "");

$catName=mysql_real_escape_string($_REQUEST['catName']);


$sql="SELECT * FROM linktb WHERE catName='$catName'";

$json['debug'] .= "SQL query was: ".$sql."\n";
$result=mysql_query($sql);
if (!$result) {
    $json['debug'] .= "SQL query failed\n";
    $json['debug'] .= "Other output: ". ob_get_contents();
    ob_end_clean();
    die(json_encode($json));
}
$count=mysql_num_rows($result);

if($count > 0){ 
$json['retval'] = 0; 
$json['data'] = array(); 
while ($row = mysql_fetch_assoc($result)){ 
$json['data'][] = $row; 
} 
} else { 
$json['retval'] = 1; 
}

$json['debug'] .= "Other output: ". ob_get_contents();
ob_end_clean();
echo json_encode($json);
4

1 回答 1

2

var catID=e.target.text();应该var catID=$(e.target).text();.text()一个jQuery方法。

如果要遍历返回的数据,则必须在成功函数中进行。同样从您的 php 脚本中,您只从数据库中输出一行。

if($count > 0){ 
    $json['retval'] = 0; 
    $json['data'] = array(); 
    while ($row = mysql_fetch_assoc($result)){ 
        $json['data'][] = $row; 
    } 
} 
else { 
    $json['retval'] = 1; 
}
$.getJSON("get.php?catID=" + catID, function(data){ 
    if (data.data){ 
        $.each(data.data,function(index,value){ 
            var url = value.URL; 
            var title = value.title; 
            var desc = value.desc; 
            $("#"+catID).appendText('<a href="'+url+'" target="iFrame"><div class="bookmark"><p><h3>'+title+'</h3><br />'+desc+'</p></div></a>'); 
        }); 
    } 
});
于 2012-10-31T19:57:07.570 回答