Reputation: 55
I am working On a shopping cart function, but I want to send product id's to This javascript part.
$(".qtybutton").on("click", function(){
var $button = $(this);
var oldValue = $button.parent().find("input").val();
var id = $button.parent().find("key").val();
if ($button.text() === "+") {
var newVal = parseFloat(oldValue) + 1;
$.post("cart_session.php", {action:'call_this', newVal: newVal },
function(data) {
$('#results').html(data);
});
}
This is the html part.
<div class="dec qtybutton">-</div>
<form method="POST">
<input class="cart-plus-minus-box" type="integer" value="2" disabled>
</form>
<div class="inc qtybutton">+</div>
I want to will pass argument with php ex:- function(<?= $key?>)
, I want to know how do I do this?
Upvotes: 1
Views: 52
Reputation: 23654
I would suggest putting the product id in the form field as a data attribute. That way you can always associate the product id with the quantity.
$(".qtybutton").on("click", function() {
let $button = $(this), $inc = $button.closest('.q-cont').find("input")
let newVal = Math.max(0,+$inc.val() + +$button.data('inc'))
$inc.val(newVal);
let id = $inc.data('productid');
console.log('newVal:', $inc.val(), 'id:', id)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='q-cont'>
<div class="dec qtybutton" data-inc="-1">-</div>
<form method="POST">
<input class="cart-plus-minus-box" data-productid="5" type="integer" value="2" disabled>
</form>
<div class="inc qtybutton" data-inc="1">+</div>
</div>
Upvotes: 2