php - 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 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.
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 valueAnswer
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'){