2016-04-02 46 views
0

我想,如果一個PHP會話存在檢查來顯示一個模式,下面是代碼:顯示PHP中的引導模式,而無需使用觸發按鈕

<?php if(isset($_SESSION['login'])) { ?> 
    <div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
    <div class="modal-header"> 
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> 
    <h3 id="myModalLabel">Modal header</h3> 
    </div> 
    <div class="modal-body"> 
    <p>Body</p> 
    </div> 
    <div class="modal-footer"> 
    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button> 
    <button class="btn btn-primary">Save changes</button> 
    </div> 
    </div> 
    <script>$("#myModal").modal("show");</script> 
<?php } ?> 

這是行不通的。

我也提到了以前的類似問題,但他們沒有給出清晰的圖片。請幫助。

+0

可能會話未設置? – guradio

+0

我已經勾選了在if語句中放置一個echo語句。它設置了 – bargad

+1

包含js和所有相關的代碼:) – guradio

回答

0

首先,我建議到.js文件從.php 分開,因爲-sometimes-我還需要在我的js文件運行PHP代碼,這是一個小「絕招」我喜歡做的事:

該js文件,應該有擴展.php(爲了觸發php解釋器),因此文件名可能看起來像這樣:scripts.js.php。這裏我還假設你有你的主文件,例如:index.php

現在,讓我們來烤蛋糕:

<html> 
<head> 
    <!-- here include your head elements and styling --> 
    <link href="path/to/bootstrap.css" rel="stylesheet"> 
    <link href="path/to/other.css" rel="stylesheet"> 
</head> 
<body> 
<!-- here goes the body code --> 
    <!-- my practice is to include the modal code and scripts at the end of the file --> 
     <div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
     <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> 
     <h3 id="myModalLabel">Modal header</h3> 
     </div> 
     <div class="modal-body"> 
     <p>Body</p> 
     </div> 
     <div class="modal-footer"> 
     <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button> 
     <button class="btn btn-primary">Save changes</button> 
     </div> 
     </div> 

    <?php 
     $show_modal=""; 
     if(isset($_SESSION["login"])){$show_modal="1";}else{$show_modal="0";} 
     include("scripts.js.php"); 
    ?> 
    </body> 
    </html> 

這裏是我的scripts.js.php文件建議:

<script src="path/to/jquery.js"></script> 
<script src="path/to/bootstrap.js"></script> 
<script src="path/to/otherfile.js"></script> 
<script> 
$(document).ready(function(){ 
    var showModal = '<?php echo $show_modal; ?>'; 
    if(showModal=="1"){$("#myModal").modal("show");} 
}); 
</script> 

index.php文件樣本是一種始終爲我工作的解決方案...希望它也適用於您!

相關問題