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!