2014-12-08 38 views
0

我在index.php中遇到了一個jQuery問題,當我包含文件header.php時沒有運行。納入了側邊欄,但是當我按下雪佛龍時,什麼都沒有發生?它只在直接包含header_user.php而沒有檢查header.php中的UserType時才起作用。我不能直接包含header_user.php,因爲我需要基於UserType顯示的頁面。在此代碼中,假設當前UserTypeUSER。請幫忙。我不知道是什麼問題。jQuery沒有使用php運行include

的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

你確定用戶類型會話變量的設置正確,腳本不只是收盤在調用中的header.php死()?嘗試用echo替換die(),看看會發生什麼。 – John 2014-12-08 06:51:47

+0

@保羅是的。會議沒有問題。我試圖直接包含文件,而不是'header.php'到'header_user.php',jQuery工作。所以會議沒有任何問題。 – 2014-12-08 06:55:10

+1

是的,但那不會測試我在問什麼...只是爲了爭論,試着用echo替換die(),或者插入'$ UType =「USER」;'在$ UType = $ _SESSION [ '的UserType'];'。 – John 2014-12-08 06:57:02

回答

0

我只是收集了所有的來源和本地測試你的HTML。我得到了一些錯誤,所以我建議改變你的代碼如下:

的index.php(jQuery和引導腳本標籤下方感動的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> 
+0

你得到了什麼錯誤?由於我沒有得到任何:O和我的HTML在我的本地主機上工作良好 – 2014-12-08 07:32:15

+0

我添加了功能更改的說明。我更改的其他內容只是爲了與您的編程風格保持一致。祝你好運! – John 2014-12-08 07:34:36

+0

好的。我在頁面末尾加載JS代碼的原因是因爲據我所知,它會加載得更快。但因爲我只是一個初學者,感謝您的建議:) – 2014-12-08 07:36:44