redirect using button onclick

✔ Recommended Answer

You may try this (assuming this code is in a blade template):

<button type="button" onclick="window.location='{{ url("users/index") }}'">Button</button>

However, {{ url('users/index') }} will print the URL so, it'll become something like this in the browser:

<button type="button" onclick="window.location='http://example.com/users/index'">Button</button>

It means that you have a route declared something like this:

Route::get('users/index', array('uses' => 'UserController@index', 'as' => 'users.index'));

In this case, may also use:

<button type="button" onclick="window.location='{{ route("users.index") }}'">Button</button>

The output will be the same.

Source: stackoverflow.com

Answered By: The Alpha

Method #2

To redirect to another web page when a button is clicked, you can use the onclick event handler and the window.location.href property to set the URL of the destination page. Here is an example code snippet:

html
<button onclick="redirectToPage()">Go to another page</button> <script> function redirectToPage() { window.location.href = "https://example.com/destination-page"; } </script>

In this example, the button element has an onclick attribute that calls the redirectToPage() function when the button is clicked. Inside the function, the window.location.href property is set to the URL of the destination page, which causes the browser to redirect to that page. Replace "https://example.com/destination-page" with the actual URL of the page you want to redirect to.

Comments

Most Popular

PhpStorm, return value is expected to be 'A', 'object' returned

Remove Unicode Zero Width Space PHP

Laravel file upload returns forbidden 403, file permission is 700 not 755