【发布时间】:2016-04-08 21:02:42
【问题描述】:
我主要使用 jQuery 制作网站,并使用 PHP 与本地数据库进行通信。
基本上这是我网页的骨架(HTML/PHP):
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type = "text/javascript" src = "script.js"></script>
</head>
<body>
<div id="content">
</div>
</body>
</html>
并为 jQuery 使用以下代码:
$(document).ready(function()
{
var tagsQuery = "<? include 'connection.php'; $sql = 'SELECT dateofsale, eid, cid, amount FROM Sales'; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { echo '<table><tr><th>Fecha</th><th>Vendedor</th><th>Cliente</th><th>Monto</th></tr>'; while($row = mysqli_fetch_assoc($result)) { echo '<tr><td>' . $row['dateofsale'] . '</td><td>' . $row['eid'] . '</td><td>' . $row['cid'] . '</td><td>' . $row['amount'] . '</td></tr>';} echo '</table>';}else {echo '0 results';}?>";
$('#content').append(tagsQuery);
});
总而言之,我将 HTML/PHP 标记存储在 jQuery 中的一个变量中,并将此标记附加到内容 div。它在第一次加载页面时起作用,但在我单击其他按钮以显示相同的数据时不起作用(例如,如果我导航到运行此代码的页面,它不能完全工作......它只是部分工作。
你能帮忙吗?如果我不够清楚,请告诉我。谢谢!!
PS:conntection.php 只连接数据库。
【问题讨论】:
-
您无法在客户端系统上运行 php 代码。您需要将 php 文件保存在服务器上,并考虑通过 jquery 运行 AJAX 请求,并使用您所需的数据来查询数据库。
-
不要那样做 php 代码。没有充分的理由将所有代码放在
var tagsQuery内的一行中。只需执行您的 php 代码,然后在var tagsQuery = ...中回显最终代码 -
在 jQuery 中使用 PHP 代码并特别包括文件和所有 insdie javascript/jQuery.... 您可以使用 AJAX 获得相同的响应并将该响应附加到特定的 div ....
标签: php jquery html tags append