Answer the question
In order to leave comments, you need to log in
Value values in php array do not change. How to fix?
There is this kind:
<?php if (!empty($products)):?>
<?php foreach ($products as $product):?>
<div class="col-xl-4">
<div class="dishes-item">
<img src="/img/products/no-foto.png" alt="3">
<h4><?= $product->name?></h4>
<p><?= $product->description?></p>
<div class="dishes-item-details">
<span class="weight"><span class="weight-size"><?= $product->weight?></span> гр</span> <span class="sum"><?= $product->price?> руб.</span>
</div>
<div class="dishes-item-counter">
Кол/во <input type="text" value="1" id="qty" data-id="<?= $product->id?>" class="dishes-item-counter-field" />
<a class="add_to_cart" data-id="<?= $product->id?>" href="<?= \yii\helpers\Url::to(['cart/add', 'id' => $product->id])?>"><button class="btn btn-choose">Добавить</button></a>
</div>
</div>
</div>
<?php endforeach;?>
<?php else:?>
<h2>Здесь товаров пока нет</h2>
<?php endif;?>
$('.add_to_cart').on('click', function (e) {
e.preventDefault();
var id = $(this).data('id'), qty = $('#qty').val();
console.log(qty)
$.ajax({
url: '/cart/add',
data: {id: id, qty: qty},
type: 'GET',
success: function(res){
if(!res) alert(ошибка);
showCart(res);
},
error: function(){
alert('Ошибка');
}
});
});
Answer the question
In order to leave comments, you need to log in
A lot of id is a mistake, only the first input is considered and not always correct.
Rewrite to a selection like $(this).closest('.dishes-item').find('input').val()
If it doesn't help, you need Back's code.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question