PHP JS - 从JS调用include_once

So I have a custom implemented single-page system like so:

index.php

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



    include_once("footer.php");
?>

views/logged_out_index.php contains some html, that makes up a page.

So, on a button click, I run a JS function like so

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

And now I'd like to include_once another view file into the body, as include_once("views/logged_out_index.php"); does, and displays that page

Use AJAX, include it dynamically in a PHP and echo the entire page. Then you append the result to your 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();
}

And you change "yourpage" with whatever the page you want to include is.

Personally, I'd suggest using jQuery or similar to make the AJAX call. If you want to know how to, comment bellow.

Cheers