Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jakeryansmith's avatar

Hitting POST routes with link.

So I'm looking for some advice on whether this is a good idea or not. I have come across a few situation when building apps where I want to trigger a route via a link, but I want want to use a something other than a GET request. A good example of this would be logging a user out. I could do this with a GET, but I would prefer to use POST or DELETE as to avoid any chance the user could bookmark the link. So I came up with a solution. I created a small javascript function.

function sendAsPost(action)
{
    var form = $('#sendAsPostForm');
    form.attr('action', action);
    form.submit();
}

Then I add a form to my main layout so it's on every page.

<form action="" method="POST" id="sendAsPostForm">
    {!! csrf_field() !!}
</form>

Now I can call a POST route with a link from anywhere in my app as simple as this:

<a href="#" onclick="sendAsPost(' route('logout') ')">Log-out</a>

--for some reason it removes the blade syntax curly braces around the route--

This seems to work great, I was just wondering how other people have dealt with this? Or if this is a terrible idea for some reason I'm not seeing. Thanks!

0 likes
4 replies
jlrdw's avatar

Have you read docs on request, and looked at the php manual on request types and how they work.

Snapey's avatar

use a GET. it's what everyone else does?

so what if they bookmark the link? If you redirect after logout, they would have to go out of their way to bookmark the logout link (right click the link and do something with it that way)

jakeryansmith's avatar

@jlrdw I know how requests types work. That's not really what I'm asking. Thanks.

@Snapey Perhaps, that wasn't the best example. There are other use cases, such submitting a POST request with a link/button inside another form. I'm actually using it to fire an email from a dropdown menu, as it would take a lot of custom CSS work to get a form submit button to match the rest of the links and look like it belongs. You would never what to use a GET request for something destructive. Even if it's unlikely, you always run the risk someone could bookmark it or revisit it in their history. I thought this might be an easy way to submit POST requests from a link. Thanks.

Please or to participate in this conversation.