我想使用AJAX在iframe中显示图片,但我的整页每5秒加载一次,如果状态表中有任何更改,我只想加载内容。
我想我已经对 部分做了些什么。可能是我需要将PHP代码转移到某些地方或者我需要在DIV上进行一些更改。如果有人请帮助我?
main.php
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>
$(function(){
function status(){
$.ajax({
type: "GET",
url: "main.php",
success: function(data){
$('div.divGranite').html(data);
}
});
};
setTimeout(status, 5000);
});
</script>
</head>
<body>
<div id="divGranite">
<?php
//Connect to Database
$connection = mysqli_connect("localhost", "root", "pass");
if (!$connection) {
die("Error: " . mysqli_error());
}
$db_select = mysqli_select_db($connection, "database");
if (!$db_select) {
die("Error: " . mysqli_error());
}
$appID = "select * from table where state= '1' ";
$result = mysqli_query($connection,$appID);
while ($row = $result->fetch_assoc()) {
$currentID = $row['id']."<br>";
}
?>
</div>
<div id="display1">
</div>
</body>
<?php
if($currentID != $displayID){
function loadUnload($ID) {
$connection = mysqli_connect("localhost", "root", "pass");
if (!$connection) {
die("Error: " . mysqli_error());
}
$db_select = mysqli_select_db($connection, "usr_web26_1");
if (!$db_select) {
die("Error: " . mysqli_error());
}
switch ($ID) {
case $ID == 1:
echo '<div>';
echo '<iframe src="../imageview/index.php" id="imgiframe" style="position:fixed; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden; z-index:999999;"></iframe>';
echo '</div>';
break;
case $ID == 2:
echo '<div>';
echo '<iframe src="../videoview/index.php" id="imgiframe" style="position:fixed; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden; z-index:999999;"></iframe>';
echo '</div>';
break;
default:
echo "Wrong ID";
break;
}
}
?>
</html>
答案 0 :(得分:1)
对于ajax代码,请写这个
$(function(){
var lastData = '';
function status(){
$.ajax({
type: "GET",
url: "main.php",
success: function(data){
if (data != prevData) {
$('div.divGranite').html(data);
prevData = data;
}
}
});
};
setTimeout(status, 5000);
});
其余的都很好!
答案 1 :(得分:0)
这个main.php正在返回包括head-tag并将其放入divGranite的孔main.php,如果你想要一个ajax请求只返回main.php生成的一部分你需要制作两个文件:< / p>
这会让你打电话给ajax.php而不是给你想要的结果 您的 ajax.php 看起来像这样,
<?php
//Connect to Database
$connection = mysqli_connect("localhost", "root", "pass");
if (!$connection) {
die("Error: " . mysqli_error());
}
$db_select = mysqli_select_db($connection, "database");
if (!$db_select) {
die("Error: " . mysqli_error());
}
$appID = "select * from table where state= '1' ";
$result = mysqli_query($connection,$appID);
while ($row = $result->fetch_assoc()) {
$currentID = $row['id']."<br>";
}
if($currentID != $displayID) {
function loadUnload($ID) {
$connection = mysqli_connect("localhost", "root", "pass");
if (!$connection) {
die("Error: " . mysqli_error());
}
$db_select = mysqli_select_db($connection, "usr_web26_1");
if (!$db_select) {
die("Error: " . mysqli_error());
}
switch ($ID) {
case $ID == 1:
echo '<div>';
echo '<iframe src="../imageview/index.php" id="imgiframe" style="position:fixed; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden; z-index:999999;"></iframe>';
echo '</div>';
break;
case $ID == 2:
echo '<div>';
echo '<iframe src="../videoview/index.php" id="imgiframe" style="position:fixed; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden; z-index:999999;"></iframe>';
echo '</div>';
break;
default:
echo "Wrong ID";
break;
}
}