There is such a view:
<?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;?> Script to it:
$('.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('Ошибка'); } }); }); I need a get method to get the value of a value in the input field. For the array, this field accepts all incoming values, in the array it gives the value 1, despite the entered data.