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

davy_yg's avatar
Level 27

Javascript code

Hello,

I cannot understand this script:

function changeSubcat() {
    $("#subcat_id").html('<option>Loading...</option>');
    $("#subcat_id").prop('disabled', false);
    var val = $("#cat_id").val();
    $.get('/cpages/subcategories/ajax/get', {
        cat_id: val
      })
      .done(function(data) {
        $("#subcat_id").html(data);
        if(getParameterByName('subcat_id') != "") {
          if(subcat_load_first == true) {
            $("#subcat_id").val(getParameterByName('subcat_id'));
            subcat_load_first = false;
          }
        }
      });
 }

What does this means: .prop('disabled', false); ?

and also: $.get('/cpages/subcategories/ajax/get', { cat_id: val })

How about this: .done(function(data) {

Thanks in advance.

0 likes
12 replies
jlrdw's avatar

prop('disabled', false); ? It is not disabled

$.get('/cpages/subcategories/ajax/get', { cat_id: val }) Your route with a cat_id of a variable val

.done(function(data) { The success after the response return back to ajax

Haven't you worked with jQuery before.

davy_yg's avatar
Level 27

In my localhost the result only shows the option with this message: Loading...

Only on the server I can see the sub categories.

jlrdw's avatar

You must not have the data on local.

davy_yg's avatar
Level 27

I wonder where the value of data comes from?

Cronix's avatar

looks like it's returned from your ajax call.

.done(function(data) {
davy_yg's avatar
Level 27

I don't understand why in local there is no return of the ajax call.

SubCategoriesController.php

public function get_ajax()
    {
        $subcategories = SubCategories::where('cat_id', request('cat_id'))->get();
        return view('admin.products.ajax.subcategories', compact('subcategories'));
        }

admin.products.ajax.subcategories.blade.php

<option value="">Semua Sub-Kategori</option>
@foreach ($subcategories as $subcat)
<option value="{{ $subcat->subcat_id }}" @if($subcat->subcat_id == request('subcat_id')) selected @endif>
    {{ $subcat->subcat_name }}
    </option>
    @endforeach
jlrdw's avatar

You should really consider finding and taking some free jQuery tutorials.

Cronix's avatar

look at your browser developer console when making the ajax call. Does it make it?

All the code you show in your initial post...what calls that changeSubcat() function? Are you sure it's even being executed? It's just a function that doesn't do anything...unless something else calls it.

davy_yg's avatar
Level 27

I already execute it just like the online version or perhaps it has something with the url:

function getParameterByName(name, url) {
        if (!url) url = window.location.href;
        name = name.replace(/[\[\]]/g, "\$&");
        var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
            return decodeURIComponent(results[2].replace(/\+/g, " "));
    }

What the url should be on local?

It works online.

Cronix's avatar
Cronix
Best Answer
Level 67

Do you know how to read your code, or didn't you write this?

 $.get('/cpages/subcategories/ajax/get', {

That's the url it's executing a get request to.

davy_yg's avatar
Level 27

That url should be correct

web.php

Route::get('/cpages/subcategories/ajax/get', 'SubCategoriesController@get_ajax');
davy_yg's avatar
Level 27

I finally find it, it should be:

 $.get('subcategories/ajax/get', {

Checking through inspect - element -network

Please or to participate in this conversation.