jQuery没有使用php include运行

时间:2014-12-08 06:31:23

标签: javascript php jquery html twitter-bootstrap

当我包含文件index.php时,我遇到header.php中的jQuery没有运行的问题。包括导航侧边栏,但当我按下雪佛龙时,什么都没发生?仅当我直接包含header_user.php而未检查UserType中的header.php时,它才有效。我不能直接包含header_user.php,因为我需要根据UserType显示页面。在此代码中,假设当前UserTypeUSER。请帮忙。我不知道是什么问题。

的index.php

<?php
    require_once 'session.php';
    require_once 'connection.php';
?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Nav Sidebar</title>
    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="bootstrap/css/dashboard.css" rel="stylesheet">

    <script>
    var change = true;
    $(document).ready(function () {
        $('[data-toggle=offcanvas]').click(function () {
            $('.row-offcanvas').toggleClass('active');
        });
    });

    function toggle_caret() {
        $("#caret1").toggleClass("glyphicon-chevron-down", change);
        $("#caret1").toggleClass("glyphicon-chevron-left", !change);
        change = !change
    }
    </script>
    </head>

    <body>
    <?php include 'header/header.php'; ?>
    <script src="jquery-1.11.1.min.js"></script> 
    <script src="bootstrap.min.js"></script>

    </body>
    </html>

的header.php

<?php

    $UType = $_SESSION['UserType'];

    $tsql = "SELECT * FROM [dbo].[LOGIN] WHERE UserType='$UType'";
    $result = sqlsrv_query( $conn, $tsql, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));

    while($row=sqlsrv_fetch_array($result))
    {
        if($UType == "USER")
        {
            include 'header_user.php';
        }
        else if ($UType == "SUPERIOR")
        {
            include 'header_superior.php';
        }
        else if ($UType == "ADMIN")
        {
            include 'header_admin.php';
        }
        else 
        die("Not a Valid User Type.");
    }
?>

header_user.php

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
            <ul class="nav nav-sidebar">
                <li class="active"><a href="#"><span class="glyphicon glyphicon-dashboard"></span>&nbsp;&nbsp;Dashboard<span class="sr-only">(current)</span></a>
                </li>
                <li onclick="toggle_caret()"><a href="#" data-toggle="collapse" data-target="#sub1"><span class="glyphicon glyphicon-list-alt"></span>&nbsp;&nbsp;Requisition<small><span style="float:right" id="caret1" class="glyphicon glyphicon-chevron-down"></span></small></a>
                </li>
                <ul class="nav collapse" id="sub1">
                    <li><a href="#"><span class="glyphicon glyphicon-file"></span>&nbsp;&nbsp;Form</a>
                    </li>
                    <li class="nav-divider"></li>
                    <li><a href="#">Status</a>
                    </li>
                    <li><a href="#"><span class="glyphicon glyphicon-ok"></span>&nbsp;&nbsp;Approved</a>
                    </li>
                    <li><a href="#"><span class="glyphicon glyphicon-remove"></span>&nbsp;&nbsp;Rejected</a>
                    </li>
                    <li><a href="#"><span class="glyphicon glyphicon-refresh"></span>&nbsp;&nbsp;In Process</a>
                    </li>
                </ul>
            </ul>
            <ul class="nav nav-sidebar">
                <li class="nav-divider"></li>
                <li><a href="#"><span class="glyphicon glyphicon-search"></span>&nbsp;&nbsp;Search</a>
                </li>
            </ul>
        </div>

修改

我发现在我更改了下面的查询后,jQuery工作了。有人可以解释为什么会这样吗?

的header.php

<?php

    $UType = $_SESSION['UserType'];

        if($UType == "USER")
        {
            include 'header_user.php';
        }
        else if ($UType == "SUPERIOR")
        {
            include 'header_superior.php';
        }
        else if ($UType == "ADMIN")
        {
            include 'header_admin.php';
        }
        else 
        die("Not a Valid User Type.");
?>

1 个答案:

答案 0 :(得分:0)

我刚收集了所有来源并在本地测试了您的HTML。我收到了一些错误,因此我建议您将代码更改为:

index.php (在jQuery和Bootstrap脚本标记下移动了JS代码)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<?php
    require_once 'session.php';
    require_once 'connection.php';
?>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Nav Sidebar</title>
        <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
        <link href="bootstrap/css/dashboard.css" rel="stylesheet">
    </head>

    <body>
        <?php include 'header/header.php'; ?>

        <script src="jquery-1.11.1.min.js"></script> 
        <script src="bootstrap.min.js"></script>
        <script>
            var change = true;
            $(document).ready(function () {
                $('[data-toggle=offcanvas]').click(function () {
                    $('.row-offcanvas').toggleClass('active');
                });
            });

            function toggle_caret() {
                $("#caret1").toggleClass("glyphicon-chevron-down", change);
                $("#caret1").toggleClass("glyphicon-chevron-left", !change);
                change = !change
            }
        </script>
    </body>
</html>

header.php (删除了数据库代码,因为这似乎是您问题的根源)

<?php
$UType = $_SESSION['UserType'];

if($UType == "USER")
{
    include 'header_user.php';
}
else if ($UType == "SUPERIOR")
{
    include 'header_superior.php';
}
else if ($UType == "ADMIN")
{
    include 'header_admin.php';
}
else
{ 
    die("Not a Valid User Type.");
}
?>

header_user.php (添加缺少的结束div代码)

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
            <ul class="nav nav-sidebar">
                <li class="active"><a href="#"><span class="glyphicon glyphicon-dashboard"></span>&nbsp;&nbsp;Dashboard<span class="sr-only">(current)</span></a>
                </li>
                <li onclick="toggle_caret()"><a href="#" data-toggle="collapse" data-target="#sub1"><span class="glyphicon glyphicon-list-alt"></span>&nbsp;&nbsp;Requisition<small><span style="float:right" id="caret1" class="glyphicon glyphicon-chevron-down"></span></small></a>
                </li>
                <ul class="nav collapse" id="sub1">
                    <li><a href="#"><span class="glyphicon glyphicon-file"></span>&nbsp;&nbsp;Form</a>
                    </li>
                    <li class="nav-divider"></li>
                    <li><a href="#">Status</a>
                    </li>
                    <li><a href="#"><span class="glyphicon glyphicon-ok"></span>&nbsp;&nbsp;Approved</a>
                    </li>
                    <li><a href="#"><span class="glyphicon glyphicon-remove"></span>&nbsp;&nbsp;Rejected</a>
                    </li>
                    <li><a href="#"><span class="glyphicon glyphicon-refresh"></span>&nbsp;&nbsp;In Process</a>
                    </li>
                </ul>
            </ul>
            <ul class="nav nav-sidebar">
                <li class="nav-divider"></li>
                <li><a href="#"><span class="glyphicon glyphicon-search"></span>&nbsp;&nbsp;Search</a>
                </li>
            </ul>
        </div>
    </div>
</div>