我想要做的是,当我提交带有票证代码的表单时,变量应该在不刷新页面的情况下进行更新。我现在只实现了提交表单,没有页面刷新,它可以工作。如果我提交表单,然后手动刷新页面,变量就会改变,但是我可以再次提交相同的表单。所以我想阻止这件事。这是我的表格:
Form
<form id="ticket" action="prekiu-uzsakymas" method="get">
<input type="text" name="ticket">
<input id="btnSubmit" type="submit" value="Panaudoti kuponą">
</form>这是我的AJAX
$(document).ready(function(){
$('#ticket').on('submit',function(e){
$.ajaxSetup({
headers: {
'X-CSRF-Token': $('meta[name="csrf_token"]').attr('content')
}
});
e.preventDefault(e);
$.ajax({
method:"GET",
url: $("#ticket").attr("action"),
data:$(this).serialize(),
dataType: 'html',
success: function(data) {
var statusHTML = '<h4><span class="label label-success">' +
'<i style="color: white" class="glyphicon glyphicon-ok"></i> Ticket used successfuly.</span></h4><br>';
$("#ticket").replaceWith(statusHTML);
},
error:function(data){
console.log("request failed");
}
})
});
});这就是当我提交带有页面刷新的表单时执行的内容:
foreach ($products as &$item) {
$item['price'] = $item['price'] - ($item['price'] * ($ticket->discount / 100));
}
Session::put('cart', $products);这一切都能工作,但我不知道如何在不刷新页面的情况下更新值,这样用户就可以看到并且不能第二次提交表单。
发布于 2016-06-08 10:17:45
将窗体包装在会话变量检查中。
form
@if ( !request()->session()->get('ticket') )
<form id="ticket" action="prekiu-uzsakymas" method="POST">
<input type="text" name="ticket">
<input id="btnSubmit" type="submit" value="Panaudoti kuponą">
</form>
@else
<h4>
<span class="label label-success">
<i style="color: white" class="glyphicon glyphicon-ok"></i> Ticket used successfully.
</span>
</h4>
<br>
@endif这将防止在会话中存储折扣票时显示表单。
然后在代码中设置以下内容以及购物车内容:
// Check for ticket being sent in request, then check the session to make sure it's not set
if ( request()->input('ticket') && (!request()->session()->get('ticket') ) ) {
foreach ($products as &$item) {
$item['price'] = $item['price'] - ($item['price'] * ($ticket->discount / 100));
}
Session::put('cart', $products);
Session::put('ticket', request()->input('ticket'));
}
// Return your altered products in your response,
// so you can update the info presented to the user
return response()->json(['products' => $products]);在你的JavaScript/AJAX里
$(document).ready(function(){
$.ajaxSetup({
headers: {
'X-CSRF-Token': $('meta[name="csrf_token"]').attr('content')
}
});
$('#ticket').on('submit',function(e){
e.preventDefault();
$.ajax({
method: $("#ticket").attr("method"),
url: $("#ticket").attr("action"),
data:$(this).serialize(),
dataType: 'html',
success: function(data) {
var statusHTML = '<h4><span class="label label-success">' +
'<i style="color: white" class="glyphicon glyphicon-ok"></i> Ticket used successfully.</span></h4><br>';
$("#ticket").replaceWith(statusHTML);
// data will now contain your products with updated prices
// You can access this with: data.products
// Example:
if (data.products) {
$(data.products).each( function(index) {
$("#product-"+$(this).id).find(".price").html($(this).price);
});
}
},
error: function(data){
console.log("request failed");
}
})
});
});我还将表单方法修改为POST,这是可选的。如果这样做,您需要将routes.php中的这个路径更改为Route::post。否则,您可以使用GET。
https://stackoverflow.com/questions/37698397
复制相似问题