简体   繁体   中英

Laravel One Form Two Submit Buttons

I have my form view;

<form class="form-horizontal" role="form" method="GET" action="">

@foreach($input as $key => $value)
    @foreach($value as $subkey => $subvalue)                            
        {!! Form::hidden($key.'_'.$subkey, $subvalue) !!}
    @endforeach
@endforeach

{!! Form::hidden('postcode_id', $postcode_lookup['id']) !!}

<input class="btn btn-info"    type="submit" name="action" value="Map View"     />
<input class="btn btn-warning" type="submit" name="action" value="CSV Download" />

</form>

I have my Routes.php code;

if ( Input::get('action') === 'CSV Download' )
{
  Route::get('/export/csv', 'ExcelController@index');
}

if ( Input::get('action') === 'Map View' )
{
  Route::get('/map/all', 'MapController@index');
}

If the user clicks one button, I want them the ExcelController called. If it's the other, I want MapController called... Two different Controllers!

What should my action route be? At the moment it just stays on the current page when I click either.

The action route is empty, so directs to the current page. It should be to some route. For example

<.. action="{{ URL::to('split_form') }}" ..>

You could then attach a filter to your route (in routes.php):

Route::get('split_form', array('before' => 'form_splitter'));

And define this filter (in filters.php)

Route::filter('form_splitter', function()
{
    if ( Input::get('action') === 'CSV Download' )
    {
        Redirect::to('/export/csv');
    }
    elseif ( Input::get('action') === 'Map View' )
    {
       Redirect::to('/map/all');
    }
});

And define the actual routes (in routes.php)

Route::get('/export/csv', 'ExcelController@index');
Route::get('/map/all', 'MapController@index');

Though the answer by angelo in blackbischop's second link could also work for what you want, saving you a filter/redirect by using javascript

you can use the same name and different value attribute for the submit buttons

// example:

<input type="submit" class="btn btn-success" value="save and close" name="submitbutton">
<input type="submit" class="btn btn-success" value="apply" name="submitbutton">
            

// Controller:

switch($request->submitbutton) {

    case 'save and close': 
        //action save here and close
    break;

    case 'apply': 
        //action for save and route here
    break;
}

or

    if ($request->submitbutton == 'apply') {
        return redirect()->route('admin.products.edit', $product->id)->with('success', "new product {$product->name} created as well.");
    } else if ($request->submitbutton == 'save and close'){
        return redirect()->route('admin.products.index')->with('info', "product {$product->name} saved");
    } 

my full answer is here https://stackoverflow.com/a/63087033/308578

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM