Gurjit singh Gurjit singh - 3 months ago 13
Ajax Question

after including page i am facing ajax error (Not Found)

I am working on notification system. Soi am following this blog (http://www.phptutorials.club/ajax-notification-in-php-with-example/)
Please see the code.

So it's working fine it's giving me a notification. But the problem is if I include the page in another index.php page. then it's giving me (error (Not Found).

I renamed the index.php as notification.php and I am including it on my website index.php page. So after including it's giving me an error but without including it's working fine.

Please suggest I know PHP but don't know about ajax and jquery,js.

Thanks

Please see my code below.

notification.php

<style>
#notification_count {
padding: 0px 3px 3px 7px;
background: #cc0000;
color: #ffffff;
font-weight: bold;
margin-left: 77px;
border-radius: 9px;
-moz-border-radius: 9px;
-webkit-border-radius: 9px;
position: absolute;
margin-top: -1px;
font-size: 10px;
}
</style>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
function addmsg(type, msg) {

$('#notification_count').html(msg);

}

function waitForMsg() {

$.ajax({
type: "GET",
url: "select.php",

async: true,
cache: false,
timeout: 50000,

success: function(data) {
addmsg("new", data);
setTimeout(
waitForMsg,
1000
);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
addmsg("error", textStatus + " (" + errorThrown + ")");
setTimeout(
waitForMsg,
15000);
}
});
};

$(document).ready(function() {

waitForMsg();

});
</script>




<span id="notification_count"></span>
<a href="#" id="notificationLink" onclick="return getNotification()">Notifications</a>
<div id="HTMLnoti" style="textalign:center"></div>



<script>
</script>


select.php

<?php include "../../includes/db.php" ?>

<?php
$sql = "SELECT * from comments where comment_status = 'unapproved'";
global $connection;

$select_comments= mysqli_query($connection, $sql) or die('Could not look up user information; ' . mysqli_error($connection));

$count = mysqli_num_rows($select_comments);
echo $count;
mysqli_close($connection);
?>


So after including the notification.php file in index.php.it's giving me an error.
But without including this file. it's working fine.

<!-- Notification panel-->
<?php include "../includes/notification.php" ?>

Answer

First check if your include path is correct. Try:

<?php include "../../includes/notification.php" ?>

Additionally, check the path to select.php in your ajax call

$.ajax({
    type: "GET",
    url: "select.php",
    ...

Second, you should think about splitting notification.php into three parts:

  • put CSS into main .css file
  • JavaScript code should be in notification.js
  • HTML code you can leave in notification.php

Then include notification.php in the index.php after you have included .css and .js files.