A
A
Alexander Gorkin2018-07-26 22:27:21
MySQL
Alexander Gorkin, 2018-07-26 22:27:21

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


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

2 answer(s)
M
mc21, 2018-07-26
@mrbagfreeman

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.

L
lamer350, 2018-07-26
@lamer350

If I understand everything correctly, then you in the back:
It is necessary to throw in the value="1"incoming data instead of 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question