Skip to content
Advertisement

Laravel 4 – two submit buttons in one form and have both submits to be handled by different actions

I have a login form which has email and password fields. And I have two submit buttons, one for login ( if user is already registered ) and the other one for registration ( for new users ). As the login action and register action are different so I need some way to redirect the request with all the post data to its respective action. Is there a way to achieve this in Laravel 4 in pure Laravel way?

Advertisement

Answer

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

}
?>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement