【发布时间】:2018-03-08 01:22:14
【问题描述】:
我正在创建一个机票预订系统,我想将数据插入数据库。像往常一样,我使用 HTML 表单,所有数据都进入数据库,除了座位号。它不会给我一个错误。 对于座位号,我使用了 Jquery,并且我想将该 Jquery 值插入到数据库中。我不知道这是否可能。 (座位号=物品)
我该如何解决这个问题?
这是我的Seat.blade.php
#holder{
height:300px;
width:800px;
background-color:#F5F5F5;
border:1px solid #A4A4A4;
}
#place {
position:relative;
margin-top:33px;
margin-left:35px;
}
#place a{
font-size:80%;
}
#place li
{
list-style: none outside none;
position: absolute;
}
#place li:hover
{
background-color:yellow;
}
#place .seat{
background:url("img/Other/Available Seat.png") no-repeat scroll 0 0 transparent;
height:30px;
width:45px;
display:block;
}
#place .selectedSeat
{
background-image:url("img/Other/Disabled Seat.png");
}
#place .selectingSeat
{
background-image:url("img/Other/Booked Seat.png");
}
#place .row-3, #place .row-4{
margin-top:10px;
}
#seatDescription li{
verticle-align:middle;
list-style: none outside none;
padding-left:35px;
height:35px;
float:left;
font-family: Times New Roman;
font-size:120%;
color:#353c47;
}
</style>
<form class="form-horizontal" id="form1" method="POST" action="{{ route('seatsinsert') }}" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="dt"> <br>
@if(session()->has('Msg'))
<h4 class="alert alert-success"> {{ session()->get('Msg') }} </h4>
@endif
<div class="form-group row">
<label for="example-date-input" class="col-2 col-form-label">Select Date :</label>
<div class="col-10">
<input class="form-control" type="date" name="date" placeholder="mm-dd-yyyy" id="example-date-input">
</div>
</div>
<div class="form-group">
<label for="exampleSelect1">Select Time :</label>
<select name="st" class="form-control" id="exampleSelect1">
<option>10.30 am</option>
<option>1.30 pm</option>
<option>4.30 pm</option>
<option>7.30 pm</option>
</select>
</div>
</div>
<h2 style="font-size:1.2em;font-family: Times New Roman;"> Choose seats by clicking below seats :</h2>
<div id="holder">
<ul id="place">
</ul>
</div>
<input type="submit" class="btn btn-primary" id="btnShowNew" value="Continue"> <br><br>
<script type="text/javascript">
$(function () {
$('#btnShowNew').click(function () {
var str = [], item;
$.each($('#place li.' + settings.selectingSeatCss + ' a'), function (index, value) {
var item = $(this).attr('title').val();
//str.push(item);
$.ajax({
type: "post",
url: "{{ route('seatsinsert') }}",
data: {items: item}
})
});
//alert(str.join(','));
})
});
</script>
</form>
这是我的SeatController.php
public function seatsinsert(Request $request) {
$date = $request->input('date');
$st = $request->input('st');
$item = $request->input('items');
//$item = item;
$user = new Seats();
$user->date = $date;
$user->st = $st;
$user->item = $item;
$this->validate($request, [
'date' => 'required'
]);
$user->save();
$request->session()->flash('Msg', 'Successfully Inserted !!');
return redirect('Seats');
}
这是我的路线。
Route::post('seatsinsert', [
'uses' => 'SeatsController@seatsinsert',
'as' => 'seatsinsert'
]);
【问题讨论】:
-
如果你做一个控制台登录项目它会显示什么?我看到你用过 var()... 你的意思是 val() ?
-
旁注;给定足够的数据,循环内的
$.post效率可能非常低。循环、收集数据并将单个POST作为数组发送可能会更好。 -
@RăducanuIonuţ - 什么都没有。但是,当我使用 str.push(item);和警报(str.join(',')); ,它给了我选择的座位号。是的,它是 val()。
-
您在使用post方式时没有提供
token。 -
@TimLewis - 我不知道。我该怎么做??
标签: php jquery database laravel