PHP / HTML:注销按钮无法点击

时间:2015-10-16 09:47:10

标签: php html button

在我的网站上我希望在用户登录时显示“注销”按钮,并在用户未登录时显示注销按钮。

我写了这段代码:

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

按钮确实出现,但遗憾的是它们无法点击。

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:5)

您未正确转发报价

替换:

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

使用:

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

答案 1 :(得分: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中。在HTML中嵌入PHP:

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

答案 2 :(得分:0)

如果您只想将用户发送到URL目标(示例中为index.php),请使用链接,而不是重新发明链接并使用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...
}