0

Sometime i am getting error about missing post data from Ajax, this isn't happen every-time and i cant understand why i am getting error message like this in my controller

PHP Notice: Undefined index: product_id

Template

<ul class="block-price">
    <li<?php if (!$stock): ?> style="display:none" <?php endif; ?>>
        <i onclick="priceUpdate('plus');"  class="fa fa-minus price-minus" aria-hidden></i>
        <input type="text" name="quantity" value="<?php echo $minimum; ?>" size="2" id="input-quantity" class="form-control" />
        <i onclick="priceUpdate('minus');"  class="fa fa-plus price-plus" aria-hidden></i>
        <input type="hidden" name="product_id" value="<?php echo $product_id; ?>" />
    </li>
</ul>

   

jQuery

function priceUpdate(type) {
    var i = parseInt($('#input-quantity').val());

    if(type == 'plus'){
        f = Number(i + 1).toString();
    }else{
        f = Number(i - 1).toString();
    }

    $('input[name="quantity"]').val(''+f+'').trigger('change')
}
$('#product').on('input change', 'input, select', function() {
    $.ajax({
        url: 'index.php?route=priceUpdate',
        type: 'post',
        data: $('#product input[type=\'hidden\'], #product input[type=\'checkbox\']:checked, #product select'),
        dataType: 'json',
        success: function(json) {

        }
    });
});

Controller

public function priceUpdate(){
  $data = [
   'cart_id'        => false,
   'product_id'     => $this->request->post['product_id'], (error message is this lane)
   'quantity'       => $quantity
  ];
}
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • 1
    You can't send a `jQuery` object as AJAX data. You need to extract the values from those elements via something like [.serialize()](https://api.jquery.com/serialize/) – Phil Sep 09 '21 at 07:04
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Nico Haase Sep 09 '21 at 07:14
  • @Phil extracting the values is happen in $this->request->post –  Sep 10 '21 at 06:21
  • I mean client-side. You need to extract the values from the HTML form elements in order to pass them along in your AJAX request – Phil Sep 10 '21 at 06:53
  • @ivan based on you comment on my answer you need to put all above html code inside `
    `tag and then direct use `.serialize()` or `.serializeArray()` of jquery to post all data
    – Alive to die - Anant Sep 10 '21 at 11:25

0 Answers0