0

我现在有一个包含 PHP 代码的页面,我想每 3 或 2 秒自动更新一个特定的 div 标签目前我从外部 php 文件更新一个空的 div 标签但是我想从同一页面更新,这是我的代码更新外部文件:

<script>
function Ajax(){
var xmlHttp;
    try{    
        xmlHttp=new XMLHttpRequest();
    }
    catch (e){
        try{
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e){
            try{
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e){
                alert("Oops!");
                return false;
            }
        }
    }

xmlHttp.onreadystatechange=function(){
    if(xmlHttp.readyState==4){
        document.getElementById('ReloadThis').innerHTML=xmlHttp.responseText;
        setTimeout('Ajax()',2000);
    }
}
xmlHttp.open("GET","page.php",true);
xmlHttp.send(null);
}

window.onload=function(){
    setTimeout('Ajax()',2000);
}

</script> 
<div id="ReloadThis"></div>

如何在同一页面更新代码,例如我想更新这个:

    <?php 
$query5= mysql_query("SELECT * FROM `table` ORDER BY `time` DESC")or die(mysql_error());
    while($arr = mysql_fetch_array($query5)){
    $num = mysql_num_rows($query5);
    $tst = $arr['names'];
    echo '<br>'; 
?>
4

1 回答 1

2

You can put your PHP code in the same file, just use query string xmlHttp.open("GET","yourFileName.php?action=updateDiv",true); for same file and then in PHP script check query string and return content according to that-

<?php
if(isset($_GET['action']))
{
    if($_GET['action'] == "updateDiv")
    {
        $query5= mysql_query("SELECT * FROM `table` ORDER BY `time` DESC")or   die(mysql_error());
        while($arr = mysql_fetch_array($query5)){
        $num = mysql_num_rows($query5);
        $tst = $arr['names'];
        echo '<br>';
        die();
    }
}
?>
于 2013-07-05T03:54:46.743 回答