简体   繁体   English

Laravel 一表二提交按钮

[英]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;我有我的 Routes.php 代码;

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.如果用户单击一个按钮,我希望他们调用 ExcelController。 If it's the other, I want MapController called... Two different Controllers!如果是另一个,我希望 MapController 被称为...两个不同的控制器!

What should my action route be?我的action路线应该是什么? 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):然后,您可以将过滤器附加到您的路线(在 routes.php 中):

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

And define this filter (in filters.php)并定义这个过滤器(在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)并定义实际路线(在 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尽管angelo在blackbischop的第二个链接中的回答也可以满足您的需求,但使用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我的完整答案在这里https://stackoverflow.com/a/63087033/308578

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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