如何在 Laravel 中为表单设置两个提交操作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19310295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to have two submit actions for a form in Laravel?
提问by Pooria Khodaveissi
In case the title is not so clear:
如果标题不是很清楚:
I have a form and two submit buttons( or I can use one submit button and the other one could be just a button or an anchor tag) and want each one to submit my form to a different action...
我有一个表单和两个提交按钮(或者我可以使用一个提交按钮,而另一个可能只是一个按钮或锚标记),并且希望每个按钮都将我的表单提交给不同的操作...
any help is so much appreciated
非常感谢任何帮助
采纳答案by smcgovern
You could give the second submit button a HTML name, then check to see if that name is set in the POST statement.
您可以为第二个提交按钮指定一个 HTML 名称,然后检查该名称是否在 POST 语句中设置。
<input name="back" type="submit" value="Go Back">
<input name="next" type="submit" value="Continue">
回答by Mike Rockétt
You'd probably need to use jQuery to accomplish that. When you construct your form, leave out the method altogether. Then, give your two buttons and id
:
您可能需要使用 jQuery 来完成此操作。当您构建表单时,请完全忽略该方法。然后,给你的两个按钮和id
:
<button id="submit-1">Submit 1</button>
<button id="submit-2">Submit 2</button>
Then, use jQuery.post
or jQuery.ajax
to submit your form data.
然后,使用jQuery.post
或jQuery.ajax
提交表单数据。
See these articles for more info:
有关更多信息,请参阅这些文章:
回答by Uze
It might be easiest to "flash"the data.
“闪现”数据可能是最简单的。
In your routes:
在您的路线中:
Route::post('/test', 'TestController@postTest');
Route::get('/test', 'TestController@getTest');
Route::any('/testSubmit1Action', function()
{
var_dump(Input::old());
});
And then your TestController:
然后你的 TestController:
class TestController extends BaseController {
public function postTest()
{
// Refer to getTest() below for how these buttons are named.
// We can check for the existence of a certain button and process
if(Input::has('submit1'))
{
// Redirect to different route / URI
Input::flash();
return Redirect::to('/testSubmit1Action');
// Alternatively, you could process action 1 here
}
if(Input::has('submit2'))
{
// Process action 2
}
}
public function getTest()
{
// I recommend putting this in a view / blade template
// eg... return View::make('foo.bar');
echo Form::open();
echo Form::submit('Submit Action 1', array('name' => 'submit1'));
echo Form::submit('Submit Action 2', array('name' => 'submit2'));
echo Form::close();
}
}