在我的视图页面中,我有一个表格格式的复选框项目列表。当用户单击我的控制器的添加按钮时,我想发送所选复选框的ID(可能是多个id)。在控制器中,我将获取一些值并将其传递给另一个视图。 下面是我的代码
<div id="content">
<div class="container">
<div class="row">
<div class="col-md-8">
<div class="contact_form_holder">
{{ Form::open(array('url' => 'booking','METHOD' => 'POST')) }}
<div class="col-md-4">
{{form::text('checkin','',['class' => 'form-control','placeholder'=>'Check In Date','id'=>'checkin'])}}
<div id="error_datepicker" class="error">Please check again</div>
</div>
<div class="col-md-4">
{{form::text('checkout','',['class' => 'form-control','placeholder'=>'Check Out Date','id'=>'checkout'])}}
<div id="error_datepicker" class="error">Please check again</div>
</div>
<p id="btnsubmit">
<input type="submit" id="send" value="Search" class="btn btn-custom"/>
</p>
{{ Form::close() }}
</div>
<div class="col-md-12">
@if (isset($roomname))
<table class="table table-hover" data-toggle="table" id="table"
data-click-to-select="true">
<thead>
<tr>
<th data-field="state" data-checkbox="true"></th>
<th data-field="roomname">Room Name</th>
<th data-field="Desc">Description</th>
<th data-field="price">Price</th>
<th data-field="roomid" data-visible="false">Price</th>
</tr>
</thead>
<tbody>
@foreach($roomname as $value)
<tr>
<td>{!! $value->roomid !!}</td>
<td>{!! $value->roomname !!}</td>
<td>{!! $value->Desc !!}</td>
<td>{!! $value->price !!}</td>
<td>{!! $value->roomid !!}</td>
</tr>
@endforeach
</tbody>
</table>
@endif
</div>
<div class="col-md-12">
<br>
<button id="add_cart" class="btn btn-warning">Proceed to booking</a></button>
<!-- on click of this button i want the clicked item id from the table and send it to the controller and from the controller it shoud redirect the value to another page -->
</div>
</div>
我的javascript
$.ajax({
url: '/roombooking',
type: "get",
data: {id: data},
success: function (response) { // What to do if we succeed
if (data == "success") {
}
},
error: function (response) {
alert('Error' + response);
}
});
答案 0 :(得分:0)
你可以将表包装在form标签中,然后使用jquery serialize()方法获取ex的所有内容。
@if (isset($roomname))
<form id="testForm">
<table class="table table-hover" data-toggle="table" id="table"
data-click-to-select="true">
<thead>
<tr>
<th data-field="state" data-checkbox="true"></th>
<th data-field="roomname">Room Name</th>
<th data-field="Desc">Description</th>
<th data-field="price">Price</th>
<th data-field="roomid" data-visible="false">Price</th>
</tr>
</thead>
<tbody>
@foreach($roomname as $value)
<tr>
<td>{!! $value->roomid !!}</td>
<td>{!! $value->roomname !!}</td>
<td>{!! $value->Desc !!}</td>
<td>{!! $value->price !!}</td>
<td>{!! $value->roomid !!}</td>
</tr>
@endforeach
</tbody>
</table>
</form>
@endif
然后在javascript使用下面
$.ajax({
url: '/roombooking',
type: "get",
data: $('#testForm').serialize(),
success: function (response) { // What to do if we succeed
if (data == "success") {
}
},
error: function (response) {
alert('Error' + response);
}
});
确保复选框是html中的数组 因此在所有复选框名称属性应该类似
<input type="checkbox" name="selected[]" />
然后在你的laravel控制器中你会得到Request :: all()将有'selected'数组
答案 1 :(得分:0)
要从复选框传递多个ID,您可以通过数组的帮助来完成。 假设(例如)如果表单标签中包含的页面上有4个复选框,则可以实现方式 - 代码如下:
<form action="some_url" method="POST">
<input type="checkbox" name="ids[]" value="1"> My Value 1
<br />
<input type="checkbox" name="ids[]" value="2"> My Value 2
<br />
<input type="checkbox" name="ids[]" value="3"> My Value 3
<br />
<input type="checkbox" name="ids[]" value="4"> My Value 4
<br />
<input type="submit" value="Submit">
</form>
因此,如果您通过选中所有复选框提交此表单,则发布数据的输出将如下:
<?php
$inputs = request()->all();
/*
Inputs will have -
$inputs = array(
'ids' => array(1, 2, 3, 4);
);
*/
?>
因此,您将使用Laravel的request()->all()方法接收ID。 希望这能帮助您解决问题...... !!