0

http://www.queness.com/post/328/a-simple-ajax-driven-website-with-jqueryphp

上記のスクリプトを機能させようとしていますが、いくつかの変更があります。html文字列としてではなく、phpインクルードとしてロードするコンテンツが必要です。現在の私の主な問題は、コードが正しいページに向けられているとは思わないため、新しいコンテンツがないことです。

php:

switch($_GET['page'])  {
    case 'code' : $page = include ($_SERVER['DOCUMENT_ROOT'].'mysite/code.php'); break;
    case 'design' : $page = include ($_SERVER['DOCUMENT_ROOT'].'mysite/design.php'); break;
    case 'writing' : $page = include ($_SERVER['DOCUMENT_ROOT'].'mysite/writing.php'); break;

html:

<ul id="menu">
         <li><a title="index" href="#index.php" rel="ajax"><span>home</span></a></li>
         <li><a title="code" href="#code.php" rel="ajax"><span>code</span></a></li>
         <li><a title="design" href="#design.php" rel="ajax"><span>design</span></a></li>
         <li><a title="illustration" href="#illustration.php" rel="ajax"><span>illustration</span></a></li>
         <li><a title="writing" href="#writing.php" rel="ajax"><span>writing</span></a></li>
         <li><a title="links" href="#links.php" rel="ajax"><span>links</span></a></li>
         <li><a title="about" href="#about.php" rel="ajax"><span>about</span></a></li>
        </ul>

javascript:

<script type="text/javascript">

    $(document).ready(function () {

    //Check if url hash value exists (for bookmark)
    $.history.init(pageload);   

    //highlight the selected link
    $('a[href=' + document.location.hash + ']').addClass('selected');

    //Seearch for link with REL set to ajax
    $('a[rel=ajax]').click(function () {

        //grab the full url
        var hash = this.href;

        //remove the # value
        hash = hash.replace(/^.*#/, '');

        //for back button
        $.history.load(hash);   

        //clear the selected class and add the class class to the selected link
        $('a[rel=ajax]').removeClass('selected');
        $(this).addClass('selected');

        //hide the content and show the progress bar
        $('#content').hide();
        $('#loading').show();

        //run the ajax
        getPage();

        //cancel the anchor tag behaviour
        return false;
    }); 
});


function pageload(hash) {
    //if hash value exists, run the ajax
    if (hash) getPage();    
}

function getPage() {

    //generate the parameter for the php script
    var data = 'page=' + document.location.hash.replace(/^.*#/, '');
    $.ajax({
        url: "loader.php",  
        type: "GET",        
        data: data,     
        cache: false,
        success: function (html) {  

            //hide the progress bar
            $('#loading').hide();   

            //add the content retrieved from ajax and put it in the #content div
            $('#content').html(html);

            //display the body with fadeIn transition
            $('#content').fadeIn('slow');       
        }       
    });
}

    </script>
4

2 に答える 2

1

includeインクルードの成功/失敗を示すtrue/falseを除いて、何も返しません。基本的にはeval()、外部ファイルを使用することを除けば、とよく似ています。含まれているページが実際に出力を生成している場合は、最初にそれをキャプチャする必要があります。

ob_start();
include('somepage.php');
$content = ob_get_clean();

return $content;

または、インクルードがコンテンツを生成して変数に保存している場合は、次のようにします。

include('somepage.php');
echo $whatever_var_that_somepage_php_stored_the_content_in;
于 2011-11-21T21:39:49.863 に答える
1

あなたは正しい道をたどりますか?

case 'code' : $page = include($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'mysite/code.php'); break;
case 'design' : $page = include($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'mysite/design.php'); break;
case 'writing' : $page = include($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'mysite/writing.php'); break;

さまざまな環境の$_SERVER['DOCUMENT_ROOT']にはスラッシュが付いている場合と付いていない場合があるため、$ _SERVER['DOCUMENT_ROOT']のファイルを含める場合は注意が必要です。

case 'code' : $page = include ($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'mysite/code.php'); break;
case 'design' : $page = include ($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'mysite/design.php'); break;
case 'writing' : $page = include ($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'mysite/writing.php'); break;
于 2011-11-22T02:39:18.300 に答える