【问题标题】:After including page I am facing ajax error (Not Found)包含页面后,我面临 ajax 错误(未找到)
【发布时间】:2016-08-19 04:39:35
【问题描述】:

我正在开发通知系统。所以我关注这个博客(http://www.phptutorials.club/ajax-notification-in-php-with-example/)。 请看代码。

它工作正常,它给我一个通知,但问题是如果我将该页面包含在另一个 index.php 页面中,那么它会给我(错误(未找到)。

我将 index.php 重命名为 notification.php,并将其包含在我的网站 index.php 页面中。所以在包含它之后会给我一个错误但不包含它工作正常。

请建议我了解 PHP 但不了解 ajax 和 jquery,js。

请看下面我的代码。

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);
?>

所以在 index.php.it 中包含 notification.php 文件后,它给了我一个错误。 但不包括这个文件。它工作正常。

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

【问题讨论】:

  • 也添加您的代码
  • 请发布您的示例代码,以便我们为您提供帮助
  • 已添加代码。请看

标签: javascript php jquery ajax jquery-ui


【解决方案1】:

首先检查您的包含路径是否正确。试试:

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

另外,在你的 ajax 调用中检查select.php 的路径

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

其次,你应该考虑将notification.php分成三个部分:

  • 将 CSS 放入主 .css 文件中
  • JavaScript 代码应该在 notification.js 中
  • HTML 代码可以留在 notification.php 中

在你包含.css和.js文件之后,然后在index.php中包含notification.php。

【讨论】:

  • 作为一名 PHP 开发人员,我也希望看到 php 逻辑从 html 中分离出来。
  • 我同意。虽然没有注意到notification.php中的php代码。
猜你喜欢
  • 2020-07-26
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
  • 2012-09-15
  • 1970-01-01
  • 1970-01-01
  • 2016-06-22
  • 2015-03-30
相关资源
最近更新 更多