php - Undefined index notice while requesting ajax with HTML5 pushstate

281

Maybe this script is outdated, but however i want to know how to fix this problem. I am gettingNotice: 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.

608

Answer

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';
}
?>
248

Answer

Solution:

NOTICE Undefined index occurs when the index is not set and you are trying to use it.
For eg: $_GET['type'] will not be set in your case. So first you need to check if it is set or not. So you can do

If(isset ($_GET['type']) || $_GET['type']!='yourvalue'){

People are also looking for solutions to the problem: Apache does not start again after php.ini change

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.