What I want to do is when I submit my form with ticket code the variables should update without page refresh. What I only achieve now is submit form without page refresh and it works. And if I submit form and then refresh page manually the variables change, but then again I can submit the same form. So I want to prevent that. This is my form:
Form
<form id="ticket" action="prekiu-uzsakymas" method="get">
<input type="text" name="ticket">
<input id="btnSubmit" type="submit" value="Panaudoti kuponą">
</form>
This is my 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");
}
})
});
});
This is what executes when I submit form with page refresh:
foreach ($products as &$item) {
$item['price'] = $item['price'] - ($item['price'] * ($ticket->discount / 100));
}
Session::put('cart', $products);
It all works but I don't know how to update values without page refresh so the user can see and cannot submit the form second time.
$('#price').text();. If u need to do it with array of products u need to create function, which will clear all data and repopulate it. Its much easier if u use things like AngularJs