将注销操作放入<a> tag

时间:2019-01-29 22:49:06

标签: html

I want to make a logout link in my User dropdown menu.. But i cant seem to set the "action" attribute to a link tag (<a>) i dont want to put it in a form, i would like to make it when they click logout it just locks out.

This is my working logout <Button> which i do not want to use.

<form action="./includes/logout.inc.php" method="post">
    <button class="btn btn-submit" type="submit" name="logout-submit">Logout</button>
</form>

I would like to include the logout link in my Drop down:

<div class='dropdown' style='float:right;'>
   <li class='login current2'><a href='#'>Welcome</a>
      <div class='dropdown-content'>
         <ul>
            <li style='font-size:10px;'><a href='#'>My Account</a></la>
            <li style='font-size:10px;'><a href='#'>My Orders</a></la>
            <li style='font-size:10px;'><a href='#'>My Wishlist</a></la>
            <li style='font-size:10px;'><a href='#'>My Cart</a></la>
            <li style='font-size:10px;'><a href='#'>Logout</a></li>
        </ul>
    </div>
  </li>
</div>

Is there a way i can make the line:

<li style='font-size:10px;'><a href='#'>Logout</a></li>

Work the same as the button but without the <form> tag and the <button> tag?

2 个答案:

答案 0 :(得分:1)

您可以通过确保您的PHP脚本使用$ _GET而不是$ _POST轻松地做到这一点,并且只需使用以下链接即可:

<li style='font-size:10px;'><a href='./includes/logout.inc.php?logout-submit=logout'>Logout</a></li>

logout.inc.php中,您将执行以下操作:

if(isset($_GET['logout-submit']) && $_GET['logout-submit'] == 'logout'){
  // run logout code
}

答案 1 :(得分:1)

一个简单的解决方案只是根据服务器端的GET请求进行处理。

<a href="./includes/logout.inc.php?logout-submit">Logout</a>

如果要在JavaScript中执行此操作,请使用document.createElement().appendChild()

选中HTML DOM Form Object

function logout() {
  var form = document.createElement("form");
  form.action = "./includes/logout.inc.php";
  form.method = "post";
  var sbmt = document.createElement("input");
  sbmt.name = "logout-submit";
  form.appendChild(sbmt);
  document.body.appendChild(form);
  form.submit();
}
<a href='#' onclick="logout()">Logout</a>