This problem is relatively complicated, atleast to explain.
I am setting up Ajax to work fluently with my Crinsane shoppingcart-plugin for Laravel. My interface enables you to buy an item from the product-list. This is done with Ajax and works fine. When an item is inserted into your cart, a small bar on the bottom pops up with all the contents of the cart. Here there is a non-submit form (just a blank input) where you can type the desired quantity of each product. With me so far? I retrieve the cart contents with another ajax function, which also works just fine. The problem is the blank quantity update input in the bottom bar. It works, but only after I have refreshed the page. When I type a desired quantity into the input field, nothing happens. But when I refresh it, it works just as I want to.
My suspision is that the Cart session or something else needs to be properly loaded by a refresh before any live input Ajax functions can get to work. Although this seems odd considering that is the entire point of ajax.
My Ajax (for the bottom bar input):
<?php
for($i=0;$i<$cartAmount; $i++){
?>
var _token = $("input[name='_token']").val();
function insert_cartBar(qty = '')
{
$.ajax({
url: '<?php echo url('/cart/bar');?>/'+ $('#cart-product-id-<?php echo
$i;?>').val(),
method:'POST',
data:{
qty:qty,
_token:_token,
rowId: $('#cart-bar-id-<?php echo $i;?>').val(),
id: $('#cart-product-id-<?php echo $i;?>').val(),
},
dataType:'json',
success:function(data)
{
$('#cartResult').text(data.cartCount);
console.log('eyyy' + data.rowId);
},
error: function(data) {
var errors = data.responseJSON;
console.log(errors);
},
});
}
$(document).on('keyup', '#cart-<?php echo $i?>', function(){
var qty = $(this).val();
insert_cartBar(qty);
});
My Laravel Controller:
public function cartBarPost(Request $request, $id) {
$cartContent = Cart::content();
$product = Product::find($id);
$rowId = $request->rowId;
$qty = $request->qty;
if($qty != '') {
Cart::update($rowId, $qty);
}
$cartCount = Cart::count();
$data = array(
'cartCount' => $cartCount,
'id' => $id,
'rowId' => $rowId,
'qty' => $qty,
);
echo json_encode($data);
}