LuMa
LuMa

Reputation: 1763

Bootstrap logout link

I use Bootstrap in my application. After adding some design I started adding some functionality. I have a navbar with a dropdown, like here. On my Website, this also features a logout link:

<li><a href="/logout">Logout</a></li>

But my problem is, that my logout will only accept POST requests. So my simple question is: How can I make this a POST request instead of GET?

Thanks

Upvotes: 2

Views: 3670

Answers (3)

Mooseman
Mooseman

Reputation: 18891

Use

<li><a href="/logout">Logout</a><form method="post" action="logout.php"><!--Whatever must be in your form--></form></li>

and

$("a + form").click(function(){
    $(this).next('form').submit();
});

Upvotes: 2

Justinas
Justinas

Reputation: 43479

There is two ways to get it solved:

  1. Change your PHP side so it accepts $_GET request. (Tip: Use $_REQUEST to get value from $_GET or $_POST.

  2. Use jQuery/js for that (sorry, no js example).

    <a href="/logout" id="logout">Logout</a>

    // load jQuery first.
    $(document).ready(function(){
       $("#logout").click(function(){
          $.post($(this).attr("href"), function(){
              window.location = "www.example.com/login"; // or any other page after logging out.
          });
       });
    });

Upvotes: 3

Prashant Tapase
Prashant Tapase

Reputation: 2147

If you want POST request then you should use form

Example:

<form method='post' action='logout.php'>
    <input type='submit' value='Logout'>
    </form>

Upvotes: 1

Related Questions