I have a problem with refreshing the page layout. Since I have all the contents load in a div, when the page first loads it loads a default link into the content div. When the user clicks a link I want that page to be stored in a session variable($last_page), so that when the page is refreshed the same page that is currently there will show. The problem is I load the pages using jQuery/Javascript so I cannot alter the $last_page session variable in the same function. I've been trying so many things(Messing with Jquery and ajax and php) and can't get it to work. How do you guys organize your layouts and/or do you know how I can fix this? Any help or attempt is greatly appreciated!

6 Years
Discussion Span
Last Post by blocblue


When you go to load the page for the first time, can you not check the session for the current page:

$currentPage = isset($_SESSION['current_page']) ? $_SESSION['current_page'] : 'default_page_template';

If the session is set, then load the current page, otherwise, revert to your default page. Then when the link on your default page is clicked, I assume you're making an ajax request to the server. At this point, on the server side, set the current_page session variable to the page you're returning.



The thing is how would I change that while in a javascript function to change the page thats the dillema


Well you said you were using ajax, and you are therefore making requests to the server. The PHP to which you're directing requests is obviously responding with the page content that is loaded in your div. Therefore, as stated above, why do you not set the SESSION variable within the PHP script that is responding to your asynchronous requests?

I've thrown together a crude example below. When you first load page.php, the div, etc, will be outputted. If the default page content contained something like:

<p>This is another page <a class="page" href="/example" title="Example page">Example page</a></p>
// page.php script

// Check whether specific page requested, otherwise check for currently loaded page, else display default page
// Make sure you validate all incoming variables
$page = isset($_POST['page']) ? $_POST['page'] : (isset($_SESSION['page']) ? $_SESSION['page'] : 'default');
$isAjax = isset($_POST['ajax']) ? (bool)$_POST['ajax'] : false;

// Output content container if request isn't ascynchronous
if(!$isAjax) : ?>
<div id="contentContainer">
<?php endif;

// Output correct content based on $page. E.g:
echo file_get_content($page);

// Output content container if request isn't ascynchronous
if(!$isAjax) : ?>
<?php endif;
$(function() {
    $('a.page').live('click', function(event){
        var href = $(this).attr('href');

            url: 'page.php',
            data: 'ajax=1&page=' + href,
            success: function(data) {

Does this help to clarify my thought process?

Edited by blocblue: Changed click event to live use live function to ensure current and all future click events are bound to function.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.