簡體   English   中英

Laravel 一表二提交按鈕

[英]Laravel One Form Two Submit Buttons

我有我的表單視圖;

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

我有我的 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');
}

如果用戶單擊一個按鈕,我希望他們調用 ExcelController。 如果是另一個,我希望 MapController 被稱為...兩個不同的控制器!

我的action路線應該是什么? 目前,當我單擊其中一個時,它只停留在當前頁面上。

動作路由為空,所以直接跳轉到當前頁面。 應該是某條路線。 例如

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

然后,您可以將過濾器附加到您的路線(在 routes.php 中):

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

並定義這個過濾器(在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');
    }
});

並定義實際路線(在 routes.php 中)

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

盡管angelo在blackbischop的第二個鏈接中的回答也可以滿足您的需求,但使用javascript為您節省了過濾器/重定向

您可以為提交按鈕使用相同的名稱和不同的值屬性

// 例子:

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

// 控制器:

switch($request->submitbutton) {

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

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

或者

    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");
    } 

我的完整答案在這里https://stackoverflow.com/a/63087033/308578

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM