[英]Value in a hidden input is not passed to controller
I'm trying to pass the user ID through an Ajax call, but it is coming empty. 我正在尝试通过Ajax调用传递用户ID,但是它是空的。 I feel the value="{{ $user->id }}" in the input tag is incorrect, but I'm not sure.
我觉得输入标记中的值=“ {{$ user-> id}}”“不正确,但是我不确定。
The following is my input tag : 以下是我的输入标签:
<div>
<input type = "hidden" name = "id" value="{{ $user->id }}">
</div>
The following is my ajax code : 以下是我的ajax代码:
$('#grp-save').on('click', function () {
$.ajax({
method: 'POST',
url: urlGrp,
data: {uid: $('#id').val(), _token: token}
})
.done(function () {
$('#join-req').modal('hide');
});
});
The following is my controller : 以下是我的控制器:
public function jmGroup(Request $request)
{
$u_id = $request['uid'];
$grpusr = new Grpusr();
$grpusr->user_id = $u_id;
$grpusr->save();
return redirect()->back();
You don't have id
on your field, so either add this: 您的字段上没有
id
,因此请添加以下内容:
<input type="hidden" name="id" id="id" value="{{ $user->id }}">
or use this instead: 或改用它:
$('input[name=id]').val()
You're trying to select the value of a field with an id named id: 您正在尝试选择ID为id的字段的值:
$('#id').val()
However your input field does not contain an id attribute: 但是,您的输入字段不包含id属性:
<div>
<input type = "hidden" name = "id" value="{{ $user->id }}">
</div>
Adding id="id"
should fix it. 添加
id="id"
应该可以解决。
<input type = "hidden" name = "id" id="id" value="{{ $user->id }}">
You need to defined the id parameter for your hidden input field, because that is what you are referencing in your JS Code. 您需要为隐藏的输入字段定义id参数,因为这是您在JS代码中引用的内容。
<input type = "hidden" id="id" name = "id" value="{{ $user->id }}">
$('#id').val()
Here Jquery is basically looking for a element having id as id. 在这里,jQuery基本上是在寻找一个以id为id的元素。 You should give it a more meaningful name like userId because a document can not have more than 1 element with same id
您应该给它一个更有意义的名称,例如userId,因为一个文档不能有多个具有相同id的元素
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.