I am using the Laravel framework and the blade templating engine.
What I would like to do, is have 2 buttons in my view, when clicked will redirect you to another view. The code I tried was:
<button type="button" onclick="{{ Redirect::to('users.index') }}">Button</button>
By using HTML Anchor Tags <a>.. </a>, you can Redirect on a Single Button Click [html button click redirect]. To use HTML Anchor tags to redirect your User to Another page, you need to write your HTML Button between these HTML Anchor Tag's starting <a> and Closing </a> Tags.
The first way through which you can redirect from one page to another is by clicking a button. You can use a form for this purpose. The form tag in HTML has an attribute action where you can give the URL of the webpage, where you want the form button to redirect.
To make Submit button redirect to another page we can use HTML Form Tags. Which will allow us to Redirect or Open another Webpage using Submit button Click. We just need to Write our Webpage path under the HTML Form's Action attribute (We we want to Redirect). It will redirect our user to given Path/Webpage.
The plain HTML way is to put it in a <form> wherein you specify the desired target URL in the action attribute. If necessary, set CSS display: inline; on the form to keep it in the flow with the surrounding text. Instead of <input type="submit"> in above example, you can also use <button type="submit"> .
You may try this (assumed this code in 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://domain.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>
Out put will be same.
Yeah, you would need to basically use the URL helper to generate the URL (same as just putting something like http://yoursite.com/users instead of the PHP helper):
<button type="button" onclick="window.location='{{ URL::route('users.index'); }}'">Button</button>
Although I don't see why you can't just use an "a href" tag instead of the button, like this:
<a href="{{ URL::route('users.index'); }}">My button</a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With