php laravel-> 在选择更改时,提交给控制器显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22917867/
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-> on select change, submit to Controller show
提问by rrubiorr81
I must be missing something. I'm trying to get the on submit from a select, to show fields on the selected item.
我肯定错过了什么。我正在尝试从选择中获取提交,以显示所选项目上的字段。
//the view
{{ Form::open(array('route' => 'user.show', 'name' => "frm_select_client")) }}
{{Form::select('client_id', array('-1' => 'Please select...') + $usersperclient, null, array('class' => 'selectpicker', 'id' => 'client_id', 'name' => 'client_id', "onchange" => "document.frm_select_client.submit();") );}}
{{ Form::close() }}
//routes.php
Route::resource('user', 'UserController');
I looking for a way to put the URL user/id (user/1901) to then go to UserController::show(). All the examples i've been seeing go directly from a link to the show:
我正在寻找一种方法来放置 URL user/id (user/1901) 然后转到 UserController::show()。我看到的所有示例都直接来自该节目的链接:
<a class="btn btn-small btn-success" href="{{ URL::to('user/' . $value->id) }}">Show this user</a>
I know i could do this in javascript, but i'm looking to do it in the best way, since i'll be doing this probably a lot. Hope this is clear. Thx in advance!
我知道我可以在 javascript 中做到这一点,但我希望以最好的方式做到这一点,因为我可能会经常这样做。希望这很清楚。提前谢谢!
EDIT: I was looking for a way to format the form submit to accomplish the resourceful format, in this case (user/{id} for the show).
编辑:我正在寻找一种方法来格式化表单提交以完成足智多谋的格式,在这种情况下(user/{id} for the show)。
回答by The Alpha
You have to use JavaScript
to fire the select's change
event and you may try this:
你必须使用JavaScript
来触发选择的change
事件,你可以试试这个:
var select = document.getElementById('client_id');
select.addEventListener('change', function(){
this.form.submit();
}, false);
This will submit
the form
on change
event and you may also try this as well:
这将submit
是form
onchange
事件,你也可以试试这个:
var select = document.getElementById('client_id');
select.onchange = function(){
this.form.submit();
};
Using jQuery
, you may try this:
使用jQuery
,你可以试试这个:
$('#client_id').on('change', function(e){
$(this).closest('form').submit();
});
Or using inline event handling, you may try this (Add this in the attribute's array):
或者使用内联事件处理,你可以试试这个(在属性的数组中添加这个):
"onchange" => "this.form.submit()"
Above code will produce something like this:
上面的代码会产生这样的东西:
<select onchange="this.form.submit()">
To generate the url
you may use this:
要生成url
你可以使用这个:
{{ Form::open(array('url' => 'user/' . $user->id, 'name' => "frm_select_client")) }}
Also, you may use route
like this (assumed user.show
is the route name for url user/{id}
):
此外,您可以这样使用route
(假设user.show
是 url 的路由名称user/{id}
):
{{ Form::open(array('route' => array('user.show', $user->id), 'name' => "frm_select_client")) }}
Make sure you pass the $user
object to your view
when loading the form, using something like this (Basically from a Controller
):
确保在加载表单时将$user
对象传递给您view
,使用类似这样的东西(基本上来自 a Controller
):
public function edit($id)
{
$user = User::find($id);
return View::make('userform')->with('user', $user);
}
Update:
更新:
Since the user id
is being selected dynamically from the select
so you have to use JavaScript
to set the form action
with id
and in this case, you may try following approach:
由于user id
是从 动态选择的,select
因此您必须使用它JavaScript
来设置表单action
,id
在这种情况下,您可以尝试以下方法:
// Add this line before you submit the form
this.form.action = 'user/' + this.value;
// Now submit
this.form.submit();
Or using jQuery
you may try this:
或者使用jQuery
你可以试试这个:
$('#client_id').on('change', function(e){
var select = $(this), form = select.closest('form');
form.attr('action', 'user/' + select.val());
form.submit();
});
In this case, keep the form action
blank, use a blank string like url => ''
.
在这种情况下,保持表单action
空白,使用像url => ''
.
回答by user1669496
Here's a pretty easy answer that does not require the form to be submitted.
这是一个非常简单的答案,不需要提交表单。
Route to show the page
显示页面的路由
Route::get('select', function()
{
return View::make('select')->with('opts', User::lists('username', 'id'));
});
The lists()
function is amazing for setting up options in a select dropdown. It's just creating a key-value array for us right from the database.
该lists()
功能非常适合在选择下拉列表中设置选项。它只是从数据库中为我们创建一个键值数组。
Setting up the form
设置表格
{{ Form::open() }}
{{ Form::select('user', $opts, null, array('id' => 'userid')) }}
{{ Form::close() }}
<span id='username'></span>
<span id='email'></span>
Just pass the array we made with the lists()
function as the second argument when setting up our dropdown. Also made places to set the username and email just for an example. The third argument would be the default if you wanted to set one, and the 4th is setting the properties of the element. You really just need an id for this example so jQuery can easily grab onto it.
lists()
在设置下拉列表时,只需将我们使用函数创建的数组作为第二个参数传递。还提供了设置用户名和电子邮件的位置,仅作为示例。如果您想设置一个,第三个参数将是默认值,第四个参数是设置元素的属性。对于这个例子,你真的只需要一个 id,这样 jQuery 就可以很容易地抓住它。
Event handler using jQuery
使用 jQuery 的事件处理程序
$(document).ready(function() {
$('#userid').on('change', function() {
var data = {
'id': $(this).val()
};
console.log(data);
$.post('{{ route("ajax.user_select") }}', data, function(data, textStatus, xhr) {
/*optional stuff to do after success */
console.log(data);
$('#username').html(data.username);
$('#email').html(data.email);
});
});
});
Using the route()
function provided by Laravel makes it very easy to call whatever route you want from javascript. The only downside is you must place this in a blade template file. I usually create a scripts
section on my master template just for this. We will set that route up next.
使用route()
Laravel 提供的函数可以很容易地从 javascript 调用任何你想要的路由。唯一的缺点是您必须将其放在刀片模板文件中。我通常scripts
为此在我的主模板上创建一个部分。接下来我们将设置这条路线。
We are using Javascript to set the username and email pages on our view to the value of the selected user.
我们正在使用 Javascript 将视图上的用户名和电子邮件页面设置为所选用户的值。
Route being called by the Javascript event handler
由 Javascript 事件处理程序调用的路由
Route::post('select', array('as' => 'ajax.user_select', 'uses' => 'HomeController@getUser'));
Very easy here, it's just saying it is expecting a post and it tells it where to route the post. The name here matches the argument we sent to the route()
function in the previous step.
在这里很容易,它只是说它正在等待一个帖子,并告诉它将帖子路由到哪里。此处的名称与我们route()
在上一步中发送给函数的参数相匹配。
Our function to get the user as was defined in the last step
我们在上一步中定义的获取用户的函数
public function getUser()
{
return Response::json(User::find(Input::get('id')));
}
Nothing difficult here. Just getting the user we wanted and returning it as a json response.
这里没什么难的。只需获取我们想要的用户并将其作为 json 响应返回。
Note:
笔记:
This method would allow people to see other user's hashed password. Even hashed, you probably don't want to show that information. To hide a piece of information when doing these things, you can add public $hidden = array('password');
to your User
model. You could also add other column names from your database as well if you wish for those to remain hidden.
这种方法将允许人们查看其他用户的散列密码。即使是散列,您可能也不想显示该信息。要在执行这些操作时隐藏一条信息,您可以添加public $hidden = array('password');
到您的User
模型中。如果您希望这些列名保持隐藏,您也可以从数据库中添加其他列名。