Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shahr's avatar
Level 10

Uncaught ReferenceError: changeQuantity is not defined

blade.php

<select onchange="changeQuantity(event, '{{ $cart['id'] }}')" class="form-select text-center" aria-label="">
    @foreach(range(1 , $product->inventory) as $item)
        <option value="{{ $item }}" {{  $cart['quantity'] == $item ? 'selected' : '' }}>
            {{ $item }}
        </option>
    @endforeach
</select>

js

<script>
    function changeQuantity(event, id , cartName = null) {
        //
        $.ajaxSetup({
            headers : {
                'X-CSRF-TOKEN' : document.head.querySelector('meta[name="csrf-token"]').content,
                'Content-Type' : 'application/json'
            }
        });

        //
        $.ajax({
            type : 'POST',
            url : '/fa/cart/quantity/change',
            data : JSON.stringify({
                id : id ,
                quantity : event.target.value,
                // cart : cartName,
                _method : 'patch'
            }),
            success : function(res) {
                location.reload();
            } 
        }); 
    }
</script>
0 likes
8 replies
Sinnbeck's avatar

Are you sure the script is included on the page? Can you run the script from the browser console?

Sinnbeck's avatar

@waves so it isn't on the page at all it seems. Go to the inspector and check the dom. I'm sure it's not there. Added to the wrong file?

Please or to participate in this conversation.