Search code examples
phpajaxhtmlnotice

Undefined index notice while requesting ajax with HTML5 pushstate


Maybe this script is outdated, but however i want to know how to fix this problem. I am getting Notice: Undefined index: notice/error when page requests ajax call. Everything works, just this notice/error bothers me. Maybe i am asking this wrongly, but how to fix this?

PHP

<?php
  if($_GET['type']!='ajax'){
    include 'header.php';
    echo "<div id='result-content'>";
  }
?>

content goes here

<?php
  if($_GET['type']!='ajax'){
    echo "</div>";
    include 'footer.php';
}
?>

JS

$.thisproblem = $.thisproblem || {};
$.thisproblem.loadContent = function () {
    $('.ajax-loader').show();
    $.ajax({
        url: pageUrl + '?type=ajax',
        success: function (data) {
            $('#result-content').html(data);
            $('.ajax-loader').hide();
        }
    });
    if (pageUrl != window.location) {
        window.history.pushState({path: pageUrl}, '', pageUrl);
    }
}

$("a").on('click', function (e) {
    pageUrl = $(this).attr('href');
    $.thisproblem.loadContent();
    e.preventDefault();
});

Sorry for bad english and thanks for any answers.


Solution

  • Since ?type is only set/used in your $.ajax() url, you will want to use !isset() to check if it is set before trying to check its value

    <?php
      if( !isset($_GET['type']) || $_GET['type']!='ajax'){
        include 'header.php';
        echo "<div id='result-content'>";
      }
    ?>
    
    content goes here
    
    <?php
      if( !isset($_GET['type']) || $_GET['type']!='ajax'){
        echo "</div>";
        include 'footer.php';
    }
    ?>