Laravel 4 - two submit buttons in one form and have both submits to be handled by different actions
The way I would do it
If your form is (2 buttons):
{{ Form::open(array('url' => 'test/auth')) }}
{{ Form::email('email') }}
{{ Form::password('password') }}
{{ Form::password('confirm_password') }}
<input type="submit" name="login" value="Login">
<input type="submit" name="register" value="Register">
{{ Form::close() }}
Create a controller 'TestController'
Add a route
Route::post('test/auth', array('uses' => 'TestController@postAuth'));
In TestController you'd have one method that checks which submit was clicked on and two other methods for login and register
<?php
class TestController extends BaseController {
public function postAuth()
{
//check which submit was clicked on
if(Input::get('login')) {
$this->postLogin(); //if login then use this method
} elseif(Input::get('register')) {
$this->postRegister(); //if register then use this method
}
}
public function postLogin()
{
echo "We're logging in";
//process your input here Input:get('email') etc.
}
public function postRegister()
{
echo "We're registering";
//process your input here Input:get('email') etc.
}
}
?>
My solution:
first I set up form with no action
<script>
var baseUrl = "{{ URL::to('/'); }}";
</script>
{{ Form::open(array('url' => '', 'id'=> 'test-form')) }}
{{ Form::text('username') }}
{{ Form::password('password') }}
{{ Form::password('confirm_password') }}
<input type="submit" name="login" id="login" value="Login">
<input type="submit" name="register" id="register" value="Register">
{{ Form::close() }}
for routes (routes.php):
Route::post('test/login', array('uses' => 'TestController@login'));
Route::post('test/register', array('uses' => 'TestController@register'));
controller (TestController.php)
public function login(){
//do something...
}
public function register(){
//do something...
}
Jquery: (test.js)
$('form :submit').on('click', function(event){
var a = $(this);
var form = $('form');
var action = a.attr('id');
form.attr('action', baseUrl + '/test/' + action);
form.submit();
});
ClickCoder's answer is spot-on, the only thing I would replace, to do it in an 100% "pure Laravel way" would be to use the Form Builder for the submit buttons as well.
http://laravel.com/docs/4.2/html#buttons
The first parameter is the submit's value, and you can pass its name in the following options array.
So instead of:
<input type="submit" name="login" value="Login">
<input type="submit" name="register" value="Register">
You'd do:
{{ Form::submit('Login', ['name' => 'login']) }}
{{ Form::submit('Register', ['name' => 'register']) }}
If you'd like, you could escape the if statement altogether by having them share the same name and do something like this:
html:
{{ Form::submit('Login', ['name' => 'action']) }}
{{ Form::submit('Register', ['name' => 'action']) }}
php:
public function postAuth()
{
$action = 'post' . Input::get('action'); //becomes either postLogin or postRegister
$this->$action();
}
A bit less nesting, a bit more Laravel!