2011-11-21 21 views
0

http://www.queness.com/post/328/a-simple-ajax-driven-website-with-jqueryphpPHP的包括內容不加載,可能哈希標籤/地址發行

我試圖得到上面的腳本工作,但也有一些變化。我需要的內容加載爲PHP包括,而不是一個HTML字符串。我現在的主要問題是,我不認爲代碼是指向正確的頁面,因此沒有新的內容。

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> 
+0

爲什麼有三聯「的情況下,包括文件時要小心'碼'」?更改爲「案例'設計'」和「案例'寫'」 – pleerock

+0

爲什麼連續三次使用'case'code''? –

+0

也將標題+「代碼」改爲標題=「代碼」 – pleerock

回答

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; 
+0

($ _SERVER ['DOCUMENT_ROOT']。'mysite/code.php');($ _ SERVER [ 'DOCUMENT_ROOT']。DIRECTORY_SEPARATOR.'mysite/code.php');我只是想知道它們是如何不同的?我很快就會檢查php.net,但我通常會發現它稍微超過了我的頭。 – expiredninja

+0

我沒有正確的路徑。我正在複製的評論有一個非常奇怪的網站結構,我只是假設我的也應該如此。謝謝! – expiredninja

1

include不返回任何東西,但真/假指示包括成功/失敗。基本上它很像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; 
+1

實際上,如果包含的腳本在任何函數之外都有「返回」,那麼include將返回一個值。 Zeta在其自動加載程序中使用此功能。 https://svn.apache.org/repos/asf/incubator/zetacomponents/trunk/Base/src/base_autoload.php – rrehbein