Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP/HTML:无法单击注销按钮_Php_Html_Button - Fatal编程技术网

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

PHP/HTML:无法单击注销按钮,php,html,button,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... } if(设置($_会

在我的网站上,我想在用户登录时显示一个注销按钮,在用户未登录时显示一个注销按钮

我写了这段代码:

 if(isset($_SESSION["username"])){ echo "<li class='xy'><button class='x' onclick='location.href = 'index.php';'>Logout</button></li>"; } else { my login button... } 
if(设置($_会话[“用户名]){echo”
  • 注销
  • ”;}否则{my login button…}
    按钮确实出现,但不幸的是,按钮不可单击


    如何解决此问题?

    您需要避开单引号,因为它们相互混淆

    更正代码:

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

    您没有正确地转义引号

    替换:

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


    如果您只想将用户发送到URL目标(
    index.php,在您的示例中为
    ),请使用链接,而不是重新创建链接并使用JS模拟它。将链接设置为按钮样式。(关于如何做到这一点,网上有很多CSS示例。)


    你说的“不可点击”是什么意思。请发布呈现的HTML输出(显示在浏览器中)。我的猜测是,您的报价和正确转义有问题。请尝试设置
    onclick=“alert('hit')”
    并查看是否可以单击该按钮。某些样式可能会使按钮看起来不可点击,或者在单击时阻止使用不同的样式。如果您有指向某个位置的链接,您确实应该使用链接(
    )而不是JS增强型按钮。您可以设置链接样式,使其显示为按钮。这并不能解决此问题。
    onclick='location.href = 'index.php';'
    
    onclick='location.href = \'index.php\';'
    
    <?php
    if( isset( $_SESSION["username"] ) ) {
        echo '<li class="xy"><a class="button x" href="index.php">Logout</a></li>';
    }
    else {
        // my login button...
    }