在 Laravel 5.3 中获取提交按钮的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42479897/
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
Getting value of a submit button in Laravel 5.3
提问by Muhammad Sipra
I have a form on my page
我的页面上有一个表格
<form method="post" action="{{url('/vpage')}}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="w100">
<button name="hostel1" class="submitBTN addnowBtn" type="submit" value="The Venetian"> Add Now</button>
</div><!--w100-->
</form>
I getting the request printed in my controller like
我在控制器中打印了请求,例如
public function vegaspage(Request $request){
dd($request);
die;
}
I have also have many fields on my page , when the request params comes to browser the submit button value is not coming in request Any ideas ?
我的页面上也有很多字段,当请求参数进入浏览器时,提交按钮值没有出现在请求中 有什么想法吗?
采纳答案by Alexey Mezenin
Change button code to:
将按钮代码更改为:
<input name="hostel1" value="The Venetian" type="hidden">
<button class="submitBTN addnowBtn" type="submit"> Add Now</button>
In the controller, use this to get the value:
在控制器中,使用它来获取值:
$request->hostel1
回答by Mayank Pandeyz
Inside your controller function try this:
在您的控制器功能中试试这个:
Input::get('hostel1', 'NA');
// It will return its value ie `The Venetian` otherwise `NA`
Note:The second parameter of Input::get()
is the default value.
注意:的第二个参数Input::get()
是默认值。
回答by Christophvh
Note: The easiest way to debug this, is via the Network tab in google chrome. You can see the header response data.
注意:最简单的调试方法是通过谷歌浏览器中的网络选项卡。您可以看到标头响应数据。
But the reason this is not working is probably because you are doing a POST Request . If you do a GET request you will get the value of the button.
但是这不起作用的原因可能是因为您正在执行 POST Request 。如果您执行 GET 请求,您将获得按钮的值。
An other reason could be that you are doing the submit trough javascript and doing an e.preventDefault()
in that case you are not really sending the request. so PHP doesn't get the value.
另一个原因可能是您正在通过 javascript 进行提交,并且e.preventDefault()
在这种情况下您并没有真正发送请求。所以 PHP 没有得到这个值。
回答by shillary
I realized that anytime I try to set a name and value to a "submit" button, laravel doesn't retrieve the values in the request. So you might want to use a hidden field like this:
我意识到每当我尝试为“提交”按钮设置名称和值时,laravel 都不会检索请求中的值。所以你可能想使用这样的隐藏字段:
<input type="hidden" name="hostel1" value="hostel1">
and retrieve it on the server side like this:
并像这样在服务器端检索它:
$request->hostel1;
回答by EddyTheDove
Do
做
$request->hostel1
When you want to dd()
your input params, do
当您想要dd()
输入参数时,请执行
dd($request->all());
回答by Sagar Patel
回答by Sagar Arora
By using request namespace you can get value of any input parameters and also same for the submit button.
通过使用请求命名空间,您可以获得任何输入参数的值,并且提交按钮也是如此。
For this you have to include Request in controller like:
为此,您必须在控制器中包含请求,例如:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Store a new user.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$name = $request->input('name');
//IN your problem you can submit button value as
$submit_button = $request->input('hostel1');
}
}
For more details about the request you can follow:
有关请求的更多详细信息,您可以关注:
https://laravel.com/docs/5.3/requests
https://laravel.com/docs/5.3/requests
Thanks
谢谢