2014-09-29 69 views
1
include_once

所以我有一個自定義實現單頁系統,像這樣:PHP JS - 從JS調用

的index.php

<?php 
    include_once('header.php'); 
    if(empty($_SESSION['loggedin'])) { 
     include_once("views/logged_out_index.php"); 
    } 



    include_once("footer.php"); 
?> 

的意見/ logged_out_index.php包含一些HTML,這構成了一個頁。

因此,在點擊一個按鈕,我運行一個JS的功能,像這樣

function showView(view) { 
    document.body.innerHTML=''; 
} 

現在我想include_once另一個視圖文件進入體內,爲include_once("views/logged_out_index.php"); 做,並顯示該頁面

回答

0

使用AJAX,將其動態包含在PHP中並回顯整個頁面。然後你將結果附加到你的innerHTML中。

FILE:retrieve_page.php

<?php 
    readfile($_GET['page']); 
?> 

的Javascript:

function loadXMLDoc() 
{ 
var xmlhttp; 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.body.innerHTML=xmlhttp.responseText; 
    } 
    } 
xmlhttp.open("GET","retrieve_page?page=yourpage",true); 
xmlhttp.send(); 
} 

並更改 「yourpage」 不管你要包含的頁面。

個人而言,我會建議使用jQuery或類似的AJAX調用。如果你想知道如何評論下面的話。

乾杯