2015-10-16 167 views
-1

在我的網站我想說明當用戶登錄的退出按鈕和註銷按鈕時未登錄用戶PHP/HTML:退出按鈕無法點擊

我寫了這個代碼:

if(isset($_SESSION["username"])){ echo "<li class='xy'><button class='x' onclick='location.href = 'index.php';'>Logout</button></li>"; } else { my login button... } 

這些按鈕確實出現,但不幸的是它們不可點擊。

我該如何解決這個問題?

+1

你是什麼意思「不可點擊」。請發佈呈現的HTML輸出(如在瀏覽器中顯示的那樣)。我的猜測是,你的報價和正確的轉義有問題。 – feeela

+0

嘗試設置'onclick ='alert('hit')「',看看你是否可以點擊按鈕。 某些樣式可以使按鈕看起來不可點擊或在點擊時防止出現不同的樣式 – x13

+0

如果您有鏈接導致到某處,您應該使用鏈接(')而不是JS增強按鈕。您可以將鏈接設置爲按鈕。 – feeela

回答

2

你需要避免單引號,因爲他們正在互相混淆。

更正代碼:

<?php 
if (isset($_SESSION["username"])){ 
    echo "<li class='xy'><button class='x' onclick='location.href = \'index.php\';'>Logout</button></li>"; 
} 
else { 
//my login button... 
} 

另一種解決方案可以不要用PHP編寫HTML。 PHP和HTML可以彼此嵌入,沒有任何限制。 您正在將HTML嵌入到PHP中。 PHP嵌入HTML:

<?php 
if (isset($_SESSION["username"])){ 
?> 
<li class='xy'><button class='x' onclick="location.href = 'index.php'">Logout</button></li> 
<?php 
} 
else { 
//my login button... 
} 
4

你是不是正確轉義報價

替換:

onclick='location.href = 'index.php';' 

有:

onclick='location.href = \'index.php\';' 
+0

這不能解決問題。 – coder

0

如果你只是想給用戶發送到URL目標(在您的示例中爲index.php)使用鏈接,而不是重新創建鏈接並使用它進行模擬ng JS。將鏈接設置爲看起來像一個按鈕。 (關於如何做到這一點,網上有很多CSS例子。)

<?php 
if(isset($_SESSION["username"])) { 
    echo '<li class="xy"><a class="button x" href="index.php">Logout</a></li>'; 
} 
else { 
    // my login button... 
}