#How to trigger routes from button click?

11 messages · Page 1 of 1 (latest)

swift sleet
#

Hello. I am new to Laravel. I often see the action attrubute of forms tag used to trigger routes. Is there any way to do so using buttons only? Like when I click on a button, it takes me to the expected route? Thanks in advance.

        <form action="{{ route("logout") }}" method="post">
            @csrf
            <button class="w-full hover:bg-gray-100 hover:text-red-500 py-2 px-4 mt-3 rounded inline-flex items-center">
            <svg xmlns="http://www.w3.org/2000/svg" width="17" height="17" fill="currentColor" class="bi bi-box-arrow-right mr-3" viewBox="0 0 16 16">
                <path fill-rule="evenodd" d="M10 12.5a.5.5 0 0 1-.5.5h-8a.5.5 0 0 1-.5-.5v-9a.5.5 0 0 1 .5-.5h8a.5.5 0 0 1 .5.5v2a.5.5 0 0 0 1 0v-2A1.5 1.5 0 0 0 9.5 2h-8A1.5 1.5 0 0 0 0 3.5v9A1.5 1.5 0 0 0 1.5 14h8a1.5 1.5 0 0 0 1.5-1.5v-2a.5.5 0 0 0-1 0z"/>
                <path fill-rule="evenodd" d="M15.854 8.354a.5.5 0 0 0 0-.708l-3-3a.5.5 0 0 0-.708.708L14.293 7.5H5.5a.5.5 0 0 0 0 1h8.793l-2.147 2.146a.5.5 0 0 0 .708.708z"/>
            </svg>
          <span>Logout</span>
        </button>
        </form>
quiet marlin
#

did you set correctly Route::post('logout', [**Controller::class, 'function name']->name('logout')) at web.php

wind plover
#

Add type="submit" to button

swift sleet
#

But suppose the button is not wrapped in a form

#

Suppose this button -

<button class="w-full hover:bg-gray-100 hover:text-red-500 py-2 px-4 mt-3 rounded inline-flex items-center">
  // some svg here 
  <span>Logout</span>
</button>
#

How do I trigger route("logout") using this button only?

quiet marlin
#

I can do. I can use jquery.

#

such as $('#button id').click(----)

wind plover