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!

Hi,

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.

R.

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>
<?php
// page.php script

session_start();
// 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) : ?>
</div>
<?php endif;
$(function() {
    $('a.page').live('click', function(event){
        event.preventDefault();
        var href = $(this).attr('href');

        $.ajax({
            url: 'page.php',
            data: 'ajax=1&page=' + href,
            success: function(data) {
                $('div#contentContainer').html(data);
            }
        });
    });
});

Does this help to clarify my thought process?
R.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.