A
A
Alexander Gorkin2018-08-05 16:45:18
Yii
Alexander Gorkin, 2018-08-05 16:45:18

Value does not accept incoming values, how can I fix this problem?

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;?>

Script for 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 the get method to get the value value in the input field.
Behind the array, this field accepts all incoming values, in the array it returns the value 1, regardless of the entered data.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Gorkin, 2018-08-05
@mrbagfreeman

$(this).closest('.dishes-item').find('input').val() Should have been rewritten to select.
I solved my own issue.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question