why do everytime i success ajax request, the output will execute for each of my php while? i just want one of them which has uniq id_cart execute.
HTML :
<ul>
<?php while { ?>
<li class="changeweight">
<form>
<select name="changeq" class="<?php echo $datacart['id_cart']; ?>">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</form>
</li>
<?php } ?>
</ul>
AJax :
$("select[name=changeq]").change(function() {
var selectq = $(this).val();
var selectidcart = $(this).attr("class");
$.ajax({
context : this,
type : "GET",
url : "ajax/changequantity.php",
dataType : "json",
data : {idcart : selectidcart, q : selectq},
success : function(changeq) {
$("li.changeweight").hide().html(changeq.totalweight).fadeIn('slow');
}
});
});
changequantity.php :
<?php
$q = $_GET['q'];
$idcart = $_GET['idcart'];
mysqli_query($connect,"UPDATE cart SET quantity = '$q' WHERE id_cart = '$idcart'");
$cart = mysqli_query($connect,"SELECT * FROM cart WHERE id_cart = '$idcart'");
$datacart = mysqli_fetch_assoc($cart);
$product = mysqli_query($connect,"SELECT * FROM product WHERE id_product = '$datacart[id_product]'");
$dataproduct = mysqli_fetch_assoc($product);
$totalweight = $datacart['quantity'] * $dataproduct['weight'];
echo json_encode(array("totalweight" => $totalweight));
?>
everything works good, the data store on my database, but the problem was ajax success will do for every php while li.changeweight.
what's wrong here?
thanks you so much.
Hello try to work with current object and get it's parents
$("select[name=changeq]").change(function() {
var self = this;
var selectq = $(this).val();
var selectidcart = $(this).attr("class");
$.ajax({
context : this,
type : "GET",
url : "ajax/changequantity.php",
dataType : "json",
data : {idcart : selectidcart, q : selectq},
success : function(changeq) {
$(self).parent().parent().hide().html(changeq.totalweight).fadeIn('slow');
}
});
});