php Laravel 4 - 一个表单中有两个提交按钮,并且两个提交都由不同的操作处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20932543/
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
Laravel 4 - two submit buttons in one form and have both submits to be handled by different actions
提问by Abhay PS
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?
我有一个登录表单,其中包含电子邮件和密码字段。我有两个提交按钮,一个用于登录(如果用户已经注册),另一个用于注册(用于新用户)。由于登录操作和注册操作不同,因此我需要某种方式将带有所有发布数据的请求重定向到其各自的操作。有没有办法以纯 Laravel 的方式在 Laravel 4 中实现这一点?
回答by ClickCoder
The way I would do it
我会这样做的方式
If your form is (2 buttons):
如果您的表单是(2 个按钮):
{{ 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'
创建一个控制器“TestController”
Add a route
添加路线
Route::post('test/auth', array('uses' => 'TestController@postAuth'));
In TestControlleryou'd have one method that checks which submit was clicked on and two other methods for login and register
在TestController 中,您有一种方法可以检查点击了哪个提交,另外两种方法用于登录和注册
<?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.
}
}
?>
回答by angelo
My solution:
我的解决方案:
first I set up form with no action
首先我设置了没有操作的表单
<script>
var baseUrl = "{{ URL::to('/'); }}";
</script>
{{ Form::open(array('url' => '', 'id'=> 'test-form')) }}
{{ Form::text('username') }}
{{ Form::password('password') }}
{{ Form::password('confirm_password') }}
<input type="submit" name="login" id="login" value="Login">
<input type="submit" name="register" id="register" value="Register">
{{ Form::close() }}
for routes (routes.php):
对于路线(routes.php):
Route::post('test/login', array('uses' => 'TestController@login'));
Route::post('test/register', array('uses' => 'TestController@register'));
controller (TestController.php)
控制器(TestController.php)
public function login(){
//do something...
}
public function register(){
//do something...
}
Jquery: (test.js)
Jquery: (test.js)
$('form :submit').on('click', function(event){
var a = $(this);
var form = $('form');
var action = a.attr('id');
form.attr('action', baseUrl + '/test/' + action);
form.submit();
});
回答by Johnny Magrippis
ClickCoder's answer is spot-on, the only thing I would replace, to do it in an 100% "pure Laravel way" would be to use the Form Builder for the submit buttons as well.
ClickCoder 的答案是准确的,我唯一要替换的是,要以 100%“纯 Laravel 方式”进行操作,也将使用表单生成器作为提交按钮。
http://laravel.com/docs/4.2/html#buttons
http://laravel.com/docs/4.2/html#buttons
The first parameter is the submit's value, and you can pass its name in the following options array.
第一个参数是提交的值,您可以在以下选项数组中传递其名称。
So instead of:
所以而不是:
<input type="submit" name="login" value="Login">
<input type="submit" name="register" value="Register">
You'd do:
你会这样做:
{{ Form::submit('Login', ['name' => 'login']) }}
{{ Form::submit('Register', ['name' => 'register']) }}
If you'd like, you could escape the if statement altogether by having them share the same name and do something like this:
如果您愿意,您可以通过让它们共享相同的名称并执行以下操作来完全转义 if 语句:
html:
html:
{{ Form::submit('Login', ['name' => 'action']) }}
{{ Form::submit('Register', ['name' => 'action']) }}
php:
php:
public function postAuth()
{
$action = 'post' . Input::get('action'); //becomes either postLogin or postRegister
$this->$action();
}
A bit less nesting, a bit more Laravel!
少一点嵌套,多一点 Laravel!
回答by Anam
You can do like the following:
您可以执行以下操作:
Your routes (routes.php):
您的路线(routes.php):
Route::post('post1', array('before' => 'checkForm', 'uses' => 'TestController@post1'));
Route::get('post2', array('as' => 'post2', 'uses' => 'TestController@post2'));
filters.php
过滤器.php
Route::filter('checkForm', function()
{
if(Input::has('button2')) {
return Redirect::route('post2')->withInput();
}
});
TestController.php
测试控制器.php
public function post1(){
return Input::all();
}
public function post2(){
return Input::old();
}
Form:should be in your view.however, I have created a temp form in route file for this answer.
表单:应该在您的视图中。但是,我在路由文件中为此答案创建了一个临时表单。
Route::get('form2', function()
{
return Form::open(array('url' => URL::to('post1'))).
Form::text('name', null).
Form::email('email', null).
'<button type="submit" name="button1" value="1">submit</button>'.
'<button type="submit" name="button2" value="2">submit2</button>'.
Form::close();
});
now browse:
现在浏览:
http://yourDomain/form2
Please note:I am not sure this is the right approach to allow two submit buttons but it work for two submit buttons.
请注意:我不确定这是允许两个提交按钮的正确方法,但它适用于两个提交按钮。
回答by mandeeya
Give the submit buttons different names, eg
给提交按钮不同的名字,例如
<button type="submit" name="login">Login</button>
<button type="submit" name="signup">Signup</button>
In your controller, simply handle login or signup depending on which button was submitted. You will still get all your inputs, you can do this;
在您的控制器中,只需根据提交的按钮处理登录或注册。你仍然会得到你所有的输入,你可以这样做;
public function postLoginOrSignup(){
$data = Input::all();
if(isset($data['login'])){
//handle login process
}
if(isset($data['signup'])){
//handle singup process
}
}

