0

I have a project in which different pages on a website will load into one wrapper; when clicking on a page link, the content is that page is loaded into the wrapper instead of the user being taken to a new page. The site is being built with PHP and MySQL, and the page is not supposed to reload. Apparently, it's supposed to be one smooth motion.

I've run through a few options that don't quite cut it. I've considered just including all files and having them within divs with no display, so that when a page is selected JavaScript will update the div display to visible. The problem with this is that it forces ALL of the pages of be parsed, which just means superfluous database queries and a resulting slower page load time.

Another thing that I have tried is to start an AJAX call when the page link is clicked. The JavaScript will pass the page name to a PHP script, which will them set a session variable ,$_SESSION['page_name'], equal to the page name passed. Using this, I can then do something like this:

                            //assume $page is equivalent to $_SESSION['page_name']
            if(isset($page) && file_exists("pages/$page")
            {
                require_once "pages/$page";
            }

But this requires the page to be refreshed in order to work. I am trying to get this all done without the page reloading.

So now I'm a bit stuck. Any suggestions?

3 Answers 3

2

Don't use $_SESSION; just use a GET or POST parameter when you run your ajax request. I.e. instead of getting /something.php instead get /something.php?page=xyz. Then use the parameter in your PHP to determine what to require

If you do need a session variable set for later use, you can do that when processing the ajax request. I.e. reverse the order of what you're doing now

Sign up to request clarification or add additional context in comments.

Comments

1

Your control point is the client. Have JavaScript set cookies if/when successful ajax responses are triggered. When JS completes an AJAX call, set the cookie. On your click events, check for whatever cookies you need and make the appropriate AJAX calls (or not), depending on whether or not the cookie exists.

Don't put the responsibility of state handling to your server in this case.

Comments

1

JQuery.load() with a simple controller to load up views?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.