PHP&;Ajax加载问题


PHP & Ajax loading issue

我目前正在为一款名为"Eve Online"的游戏开发一个网络工具。这款游戏有一个ingame浏览器,完全支持所有HTML、CSS、PHP和Javascript语言,没有任何问题。

对于这个项目,我使用PHP和Ajax,因为我不想每次都刷新页面。我想每5秒自动刷新一次。

所以为了做一个小测试,我想显示服务器时间,并每秒刷新一次。这是我的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
<title>Eve Online - Tactical Fleet Tool</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
    function loadXmlHttp(url, id) {
        var f = this;
        f.xmlHttp = null;
        if (window.XMLHttpRequest) // && !f.ie||/^http/.test(window.location.href)
            f.xmlHttp = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari, others, IE 7+ when live - this is the standard method
        else if (/(object)|(function)/.test(typeof createRequest))
            f.xmlHttp = createRequest(); // ICEBrowser, perhaps others
        else {
            f.xmlHttp = null;
        }
        if(f.xmlHttp != null){
            f.el = document.getElementById(id);
            f.xmlHttp.open("POST",url,true);
            f.xmlHttp.onreadystatechange = function(){
                            f.stateChanged();
            };
            f.xmlHttp.send(null);
        }
    }

    loadXmlHttp.prototype.stateChanged=function () {
        if (this.xmlHttp.readyState == 4 && (this.xmlHttp.status == 200 || !/^http/.test(window.location.href)))
            this.el.innerHTML = this.xmlHttp.responseText;
    }
    var requestTime = function(){
        new loadXmlHttp('member.php', 'timeDiv');
        setInterval(function(){
            new loadXmlHttp('member.php?t=' + new Date().getTime(), 'timeDiv');
        }, 1000);
    }
    if (window.addEventListener)
     window.addEventListener('load', requestTime, false);
    else if (window.attachEvent)
     window.attachEvent('onload', requestTime);
</script>
</head>
<body>
<div id="header">
    <div id="headercontainer">
        <a href="http://www.eveonline.com">
            <img style="border:0px;" alt="EVE Online" src="images/header.jpg"/>
        </a>
    </div>
</div>
<?php
    session_start();
    if($_SESSION['username'])
    {
        if($_SERVER['HTTP_EVE_TRUSTED'] == 'No')
            header('eve.trustMe:http://localhost/EveOnline_FleetManager/::Please trust me!');
        else
        {
            $charname =  $_SERVER['HTTP_EVE_CHARNAME'];
?>
<div id="menu">
    <ul>
        <li>You are currently logged in with: <?php echo $charname ?></li>
        <li>Add Character to account</li>
        <li>Make a fleet</li>
        <li>Edit Account Details</li>
        <li>Logout</li>
    </ul>
</div>
<div id='timeDiv'>
    <?php include "test.php"; ?>
</div>
<div id='overview'>
<h2 style="text-align:center;">Fleet Overview</h2>
<table id='fleet_table' border="1" align="center">
<tr id='fleet_table_header'>
    <th>Fleetname</th>
    <th>Started by Pilot</th>
    <th>Join</th>
</tr>
<tr id='fleet_table_data'>
    <td>iLLogicaL Pwn Fleet</td>
    <td>21:00 09/01/2012 by iLLogicaL</td>
    <td>Join</td>
</tr>
</table>
</div>
<?php 
        }
    }
    else
        die("You must be logged in!");
?>
</body>
</html>

在我的test.php页面中,我只有一个返回服务器时间的echo。这段代码给出了以下结果。

http://i42.photobucket.com/albums/e326/kartingfreak/20120116201252.jpg

因此,服务器时间正在正确更新,但正如您所看到的,其他div也在刷新,并被放置在"timeDiv"中。

我做错了什么?

提前Thx。

您将用ajax响应替换timeDiv的内容。您只需要返回ajax请求的时间,而不需要返回整个页面。

当我读到它时,它看起来像是从member.php加载member.php?

例如,如果您创建了time.php,它只输出时间(而不输出其他内容),然后使用AJAX加载它,这行吗?

编辑:看起来您已经有了一个返回时间的文件(test.php)——在ajax调用中加载该文件是否有效?