如何使用php / ajax在指定的div中附加其他数据

时间:2012-06-20 18:15:40

标签: php javascript jquery ajax

我想知道是否有办法在点击链接后显示外部php文件,然后在单击其他链接后显示另一个外部文件(不是INSTEAD)。这是我的代码。

的index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html>  
<head>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-   1.2.6.pack.js"></script>  
<script type="text/javascript" src="core.js"></script>   
</head>  
<body>   
<div id="menu">  
<ul>  
<li id="home"><a href="#downloads">DOWNLOADS</a></li>  
<li id="tutorials"><a href="#errors">ERRORS</a></li>  
</ul>   
</div>  
<div id="content">    
</div>  
</body>  
</html>  

core.js

//On load page, init the timer which check if the there are anchor changes each 300 ms
$().ready(function(){
setInterval("checkAnchor()", 100);
});
var currentAnchor = null;
//Function which chek if there are anchor changes, if there are, sends the ajax    petition
function checkAnchor(){
//Check if it has changes
if(currentAnchor != document.location.hash){
    currentAnchor = document.location.hash;
    //if there is not anchor, the loads the default section
    if(!currentAnchor)
        query = "page=1";
    else
    {
        //Creates the  string callback. This converts the url URL/#main&id=2 in URL/?section=main&id=2
        var splits = currentAnchor.substring(1).split('&');
        //Get the section
        var page = splits[0];
        delete splits[0];
        //Create the params string
        var params = splits.join('&');
        var query = "page=" + page + params;
    }
    //Send the petition
    $("#loading").show();
    $.get("callbacks.php",query, function(data){
        $("#content").html(data);
        $("#loading").hide();
    });
}
}

downloads.php

<b>DOWNLOADS</b>

errors.php

<b>ERRORS</b>

callbacks.php

<?php  
//used to simulate more waiting for load the content, remove on yor projects!  
sleep(1);  
//Captures the petition and load the suitable section  
switch($_GET['page']){  
case "errors": include 'errors.php'; break;
case "downloads": include 'downloads.php'; break;

default: include 'downloads.php'; break;

}  
?>

这完全有效,除了它使用一个开关,我希望能够同时看到errors.php和downloads.php,而不仅仅是一个或另一个。

修改

伪代码,使其更清晰:

如果点击下载,只显示download.php。如果单击错误,则仅显示error.php(在downloads.php之后)并且不删除downloads.php或主页上可能包含或可能不包含的任何其他外部文件。

有什么建议吗?

P.S。我已经浏览了很多关于这个问题的线索,这就是为什么我不能发布我尝试过的所有代码(对不起,我也不能包含链接,上次我的问题是因为这样做而被忽略了...&gt; :/)所以我可以保证我已经完成了我的作业。

p.s.s。如果你认为这值得投票,请善意解释原因。我很容易受到批评,但只是竖起大拇指根本没有帮助。

修改

core.js 更新为

$(document).ready(function(){
$('#menu li a').click(function() {
    var currentAnchor = $(this).attr('href');
    if(!currentAnchor)
        var query = "page=1";
    else
    {
        var splits = currentAnchor.substring(1).split('&');
        //Get the section
        var page = splits[0];
        delete splits[0];
        //Create the params string
        var params = splits.join('&');
        var query = "page=" + page + params;
    }
    //Send the petition
    $("#loading").show();
    $.get("callbacks.php",query, function(data){
        $("#content").html(data);
        $("#loading").hide();
    });       
    return false;
});
});

2 个答案:

答案 0 :(得分:0)

如果你想一次显示两个页面,在你的callbacks.php页面中你应该可以做这样的事情(我所做的就是删除switch语句):

include 'errors.php';
include 'downloads.php';

为什么你不能这样做?

答案 1 :(得分:0)

编辑:

[这里删除了令人困惑的部分]

-

编辑:

core.js (已修订)

//On load page, init the timer which check if the there are anchor changes each 300 ms
$(document).ready(function(){
    $('#menu li a').click(function() {
        var currentAnchor = $(this).attr('href');
        if(!currentAnchor)
            var query = "page=1";
        else
        {
            var splits = currentAnchor.substring(1).split('&');
            //Get the section
            var page = splits[0];
            delete splits[0];
            //Create the params string
            var params = splits.join('&');
            var query = "page=" + page + params;
        }
        //Send the petition
        $("#loading").show();
        $.get("callbacks.php",query, function(data){
            $("#content").html(data);
            $("#loading").hide();
        });       
        return false;
    });
}​​​);​​​

-

编辑:

这个将&#34; 追加&#34;数据[来自下载错误]到现有内容。

        $.get("callbacks.php",query, function(data){
            $("#content").append(data);
            $("#loading").hide();
        }); 

希望这有帮助。