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.
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.
In my localhost the result only shows the option with this message: Loading...
Only on the server I can see the sub categories.
You must not have the data on local.
I wonder where the value of data comes from?
looks like it's returned from your ajax call.
.done(function(data) {
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
You should really consider finding and taking some free jQuery tutorials.
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.
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.
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.
That url should be correct
web.php
Route::get('/cpages/subcategories/ajax/get', 'SubCategoriesController@get_ajax');
I finally find it, it should be:
$.get('subcategories/ajax/get', {
Checking through inspect - element -network
Please sign in or create an account to participate in this conversation.