0

我看本教程 http://tutorialzine.com/2009/09/simple-ajax-website-jquery/ 但我不明白如何使用带有 sql 的 php 文件进行这项工作,echo ""; 等如果有人可以解释,我会尝试一切,但没有出现谢谢:)

var default_content = "";

$(document).ready(function () {

    checkURL();
    $('ul li a').click(function (e) {

        checkURL(this.hash);

    });

    //filling in the default content
    default_content = $('#pagesContent').html();


    setInterval("checkURL()", 250);

});

var lasturl = "";

function checkURL(hash) {
    if (!hash) hash = window.location.hash;

    if (hash != lasturl) {
        lasturl = hash;

        // FIX - if we've used the history buttons to return to the homepage,
        // fill the pageContent with the default_content

        if (hash == "")
            $('#pagesContent').html(default_content);

        else
            loadPage(hash);
    }
}


function loadPage(url) {
    url = url.replace('#page', '');

    $('#loading').css('visibility', 'visible');

    $.ajax({
        type: "POST",
        url: "load_page.php",
        data: 'page=' + url,
        dataType: "html",
        success: function (data) {
            if (parseInt(data) != 0) {
                $('#pagesContent').html(data);
                $('#loading').css('visibility', 'hidden');
            }
        }
    });

}

load_page.php

<?php
if(!$_POST['page']) die("0");
$page = (int)$_POST['page'];
if(file_exists('pages/page_'.$page.'.php'))
echo file_get_contents('pages/page_'.$page.'.php');
else echo 'There is no such page!';
?>

演示.html

< a href="#page1">Page1< /a>
< a href="#page2">Page2< /a>
< a href="#page3">Page3< /a>
< a href="#page4">Page4< /a>
<div id="pageContent">
   //loaded ajax page
</div>

在这种情况下,link > index.html#page1 将加载文件'pages/page_1.php',但在主 index.html 中只能加载 html 代码,而不是 php 语法。在这种情况下我可以使用 php 命令吗?

4

1 回答 1

0

所以看起来这里的问题是在教程示例中,他们只是使用了一个 HTML 文件。为此,file_get_contents()将正常工作。但是,如果您希望您的服务器在将 PHP 代码提供给用户之前对其进行解析,您应该使用include()函数.

从文档中

include 语句包含并评估指定的文件。

file_get_contents()函数的行为略有不同:

file_get_contents— 将整个文件读入字符串

它只是读取文件的内容,不评估/执行/解释 PHP 代码...

于 2013-01-29T17:21:55.247 回答