>我正在尝试从mysql获取信息并将信息发布到html页面中。以下是我到目前为止得到的:这是我的tenantlistmob.php
<?php
include('connection.php');
$result = mysql_query("SELECT * FROM tenanttemp");
while ($row = mysql_fetch_assoc($result))
{
$array[] = array($row['TenantFirstName']);
}
echo json_encode($array);
?>
当我直接从浏览器调用tenantlistmob
时,它会[["Humayun"],["Sahjahan"],["Bayezid"],["Bayezid"],["Asaduzzaman"],["Mouri"]]
显示名字的来源。我喜欢在 html 页面中使用此名称。我的 HTML 页面是
<!DOCTYPE HTML>
<html>
<link rel="stylesheet" href="styles/main.css" />
<script type="text/javascript" src="jquery.js"></script>
<body>
<div id="output">this element will be accessed by jquery and this text replaced</div>
<script id="source" type="text/javascript">
$(function ()
{
$.ajax({
url: 'tenantlistmob.php',
data: "",
dataType: 'json',
success: function(data)
{
var id = data;
//var vname = data[1]; //get name
$.each(id, function (val)
{
$('#output').html(""+id);
});
}
});
});
</script>
<form id="formset">
<fieldset id="fieldset">
<h3 align="center">Tenant List</h3><hr/>
<a href="#">name1</a><br /><hr/>
<a href="#">name2 </a> <br /><hr/>
</fieldset>
</form>
<a id="box-link1" class="myButtonLink" href="category1.php"></a>
</div>
</body>
</html>
我的输出(main.css
)是这样的
#output
{
color:#ffffff;
font-size : 20px;
margin : 0;
letter-spacing:1px;
width:480px;
}
我得到的名字Humayun,Sahjahan,Bayezid,Bayezid,Asaduzzaman,Mouri
在左上角。但是我喜欢将名称作为带有链接的列表(name1
,name2
)。当我单击名称(name1
,name2
)时,它将显示名称的详细信息。我该怎么做?
提前致谢
看起来您希望使用 JavaScript 迭代 JSON。由于你使用的是jQuery,你只需要"迭代"JSON结果。从技术上讲,在 JavaScript 中,0 在 1 之前。
var _result = $data[0];
$.each(_result, function (val)
{
console.log(val);
});
http://api.jquery.com/jQuery.each/
试试这个:
<?php
include('connection.php');
$result = mysql_query("SELECT * FROM tenanttemp");
$array = array();
while ($row = mysql_fetch_assoc($result))
{
$array[] = $row['TenantFirstName'];
}
echo json_encode($array);
?>