Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect using button onclick

Tags:

php

laravel

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>
like image 970
Chris Avatar asked Apr 09 '14 00:04

Chris


People also ask

How do I redirect a clicked 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.

Can a button redirect?

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.

How do I redirect another page when the login button is clicked?

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.

How do I link a button to a URL?

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"> .


2 Answers

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.

like image 144
The Alpha Avatar answered Oct 11 '22 16:10

The Alpha


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>
like image 32
msurguy Avatar answered Oct 11 '22 17:10

msurguy