使用 jQuery 变量存储 HTML/PHP 标签
Using jQuery variables to store HTML/PHP tags
我正在制作一个网站,主要使用 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 标签中呢? Javascript 仅用于客户端脚本。您无法从 Web 浏览器更改服务器代码。如果需要
,您可以轻松地将代码放入 php 标记中
<?php your php code here ?>
这样你就可以完成你即将完成的任务
PHP 必须在服务器上处理,当您在浏览器中 运行 jQuery 时,服务器已不在画面中。最好的办法可能是将 "tagsQuery" 代码放入 PHP 脚本中,然后使用 AJAX、API-style:
调用它
$(document).ready(function(){
$.ajax('http://example.com/new_php_script.php')
.done(function(data){
// "data" is the resulting output of the PHP script
$('#content').append(data);
});
});
你也可以这样做,如果你想将你的设计附加到以前的 div
$(document).ready(function()
{
var html = '';
html += '<div class="item col-xs-12 col-lg-6">';
html += '<div class="thumbnail">';
html += '<div class="caption">';
html += '<h4 class="group inner list-group-item-heading">';
html += '<b>'+data.subject+'</b></h4>';
html += '<h5><b>'+data.projectname+'</b></h5><br />';
$('#content').append(html);
});
我正在制作一个网站,主要使用 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 标签中呢? Javascript 仅用于客户端脚本。您无法从 Web 浏览器更改服务器代码。如果需要
,您可以轻松地将代码放入 php 标记中<?php your php code here ?>
这样你就可以完成你即将完成的任务
PHP 必须在服务器上处理,当您在浏览器中 运行 jQuery 时,服务器已不在画面中。最好的办法可能是将 "tagsQuery" 代码放入 PHP 脚本中,然后使用 AJAX、API-style:
调用它$(document).ready(function(){
$.ajax('http://example.com/new_php_script.php')
.done(function(data){
// "data" is the resulting output of the PHP script
$('#content').append(data);
});
});
你也可以这样做,如果你想将你的设计附加到以前的 div
$(document).ready(function()
{
var html = '';
html += '<div class="item col-xs-12 col-lg-6">';
html += '<div class="thumbnail">';
html += '<div class="caption">';
html += '<h4 class="group inner list-group-item-heading">';
html += '<b>'+data.subject+'</b></h4>';
html += '<h5><b>'+data.projectname+'</b></h5><br />';
$('#content').append(html);
});