我想将 MySql 数据库中的数据与一个或多个 div 中的默认内容进行交换,而无需重新加载页面。我在 Dreamweaver CS6 中使用“设置容器文本”javascript 行为插入了 PHP 代码,但没有成功。
如果有人能告诉我如何做到这一点,我将不胜感激。
save your php code in a file named getinfo.php then call it with ajax
<?php
include("connectdatabase.php");
$sql="SELECT * FROM table ORDER BY id DESC";
$result=mysqli_query($con,$sql);
$i = 1;
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $i . "</td>";
echo "<td>" . $row["Name"] . "</td>";
echo "<td>" . $row["Score"] . "</td>";
echo "</tr>";
$i++;
}
mysqli_close($con);
?>
在你的主文件中使用这个
<div class="scoreBoard"></div>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
setInterval( refreshScoreBoard, 5000 );
var inRequest = false;
function refreshScoreBoard() {
if ( inRequest ) {
return false;
}
inRequest = true;
var load = $.get('getinfo.php');
$(".scoreBoard").html('Refreshing');
load.error(function() {
console.log( "Error" );
// do something here if request failed
});
load.success(function( res ) {
console.log( "Success" );
$(".scoreBoard").html('<table>'+res+'</table>');
});
load.done(function() {
console.log( "Completed" );
inRequest = false;
});
}
</script>