2

I'm learning how to use AJAX and I made 4 files:

header.php
page1.php
page2.php
footer.php

My header contains like body, html, head and things. While my footer, </body>...
I need to make a navbar that calls page1.php trough AJAX, but what if user access page1.php directly, what happens to header and footer ;-;?
I tried to make this two things in PHP:

header.php:

<? $a = 1 ?>

page1.php:

<? if ($a != 1){include 'includes/header.php';} ?>

But page1 keeps calling header even when bloody $a IS EQUAL 1!!!!!!

I also made this in page1.php:

<? echo $a ?>

And guess what? Yes... it returns 1...

AJAX code in header.php:

$(function() {
            $('nav a').click(function(e) {

            e.preventDefault();
            var href = $(this).attr('href');

            $('#content').html('Downloading...');

            $.ajax({
                url: href,
                type: 'GET',
                error: function(){
                    // always good to have an error handler with AJAX
                },
                success: function(data){
                    $('#content').html(data);
                }
            });

                // HISTORY.PUSHSTATE
                history.pushState('', 'New URL: '+href, href);
                // e.preventDefault();


            });

HALP!

Igor
  • 131
  • 3
  • 10
  • 1
    where is ajax?kindly add – guradio Feb 18 '16 at 04:13
  • 3
    1. the question does not contain AJAX; 2. the code does not make sense. – Raptor Feb 18 '16 at 04:15
  • Well, technically it is a issue caused by AJAX. Maybe there is another way to do it without all my mess... – Igor Feb 18 '16 at 04:15
  • I will add AJAX... ---------------------- AJAX code added. Why the code doesn't make sense, Raptor? – Igor Feb 18 '16 at 04:16
  • I suggest a fifth page that always includes the header and the footer. The `page1` and `page2` type pages would only need there own content. –  Feb 18 '16 at 04:27
  • I don't get it. Again, what if user try to access `page1` and `page2` directly? – Igor Feb 18 '16 at 04:35
  • @Igor [you'd use mod_rewrite](http://stackoverflow.com/questions/9694118/rerouting-all-php-requests-through-index-php) (or similar if you're not using apache) to redirect traffic to the index page. So the user navigates to `example.com/page1.php` The server sends the request to `example.com/index.php` but with the original request as part of the query string. `index.php` see the original request was for `page1` and automatically includes it. –  Feb 18 '16 at 12:35

1 Answers1

1

If page1.php is loaded with AJAX it can not evaluate the $a variable declared in the header.php because all PHP code in page1.php is executed serverside before the response is returned to the header page. But you can however pass a variable as a parameter along with the request in the url

var href = $(this).attr('href')+'?a='+'<?php echo $a; ?>';

This will pass a parameter with value of $a with the url like this: //page1.php?a=1

In your page1.php you can then check if this variable is NOT passed (meaning header.php didn't request the page):

if (!isset($_GET['a'])){
    include 'includes/header.php';
}
Nillervision
  • 451
  • 2
  • 10