JS:
$('select#categories').change(function() {
var category = $(this).val();
$.ajax({
category: category,
url: '/categories/get-subcategories'
}).done(function(subcategories) {
// subcategories is json, loop over it and populate the subcategory select
});
});
PHP:
$subcategories = SubCategory::select('name')
->where('parent', Input::get('category'))
->get()->toArray();
return Response::json(compact('subcategories'));
Anthony only a question the php code would get the category from Input::get('subcategory') or using the category value?
thanks for the start, but i'm still running into issues. i added console.log to the following but it's returning an empty array. but when i load the view in the browser and provide a category id, it returns a json value
$('select#categories').change(function() {
var category = $(this).val();
$.ajax({
category: category,
url: '/categories/get-subcategories'
}).done(function(subcategories) {
// subcategories is json, loop over it and populate the subcategory select
console.log(subcategories);
$.each(subcategories, function(i, item)
{
subcategoryItems+= "<option value=''>" + item.subcategory_name + "</option>";
});
});
});
i'm assuming that's how i loop through the json correct? well the array is always empty when the change() occurs.
when viewing the view /resource-categories/get-resource-subcategories?category=1, i get the following
{"subcategories":[{"resource_subcategory_name":"Articles"},{"resource_subcategory_name":"Mentor-Mentee "},{"resource_subcategory_name":"Registrar"},{"resource_subcategory_name":"Peer Reviewer Application"}]}
mcraz said:
Shouldn't it be
data.subcategories
?
i get a "ReferenceError: data is not defined" when i used it with console.log(data.subcategories). i may have misunderstood you, i'm still a noob at this
edit: i see what you're saying now about data. http://api.jquery.com/jquery.get/
i will try that
ok so here's where i made some adjustments
$.ajax({
data: category,
url: '/resource-categories/get-resource-subcategories'
}).done(function(subcategories) {
// subcategories is json, loop over it and populate the subcategory select
});
rather than use category:category, i replaced it with data:category. it looks like it's passing it now. my url looks like resource-categories/get-resource-subcategories?value_of_category so if category id = 9, the url is resource-categories/get-resource-subcategories?9. unfortunately, it wont work unless it's resource-categories/get-resource-subcategories?category=9. how do i get the querystring to be that?
got it! geez i was missing the top level of json. the json value looks like this
{"subcategories":[{"id":5,"resource_subcategory_name":"Articles"},{"id":7,"resource_subcategory_name":"Mentor-Mentee "},{"id":9,"resource_subcategory_name":"Registrar"},{"id":10,"resource_subcategory_name":"Peer Reviewer Application"}]}
i couldn't get the value when i used
$each(subcategories, function(i, item) {
console.log(item.resource_subcategory_name);
});
i had to make json...
result{"subcategories":[{"id":5,"resource_subcategory_name":"Articles"},{"id":7,"resource_subcategory_name":"Mentor-Mentee "},{"id":9,"resource_subcategory_name":"Registrar"},{"id":10,"resource_subcategory_name":"Peer Reviewer Application"}]}
# this works
$each(result.subcategories, function(i, item) {
console.log(item.resource_subcategory_name);
});
it's weird why i had to do that but it works now. thanks everyone
you should be able to get the correct formatting of the json with: $subcategories = SubCategory::select('name') ->where('parent', Input::get('category')) ->get()->lists('name','id');
return Response::json(compact('subcategories'));
if you want to have your query as ?category=9 you can just look for that input value in your function, soemthing like function subcategories() { $category = Input::get('category'); //..... }
wing5wong said:
What is your php for retrieving the data? you should be able to get the correct formatting with: $categories-><whatever>()->lists(name,id)
no issues with php, it was mainly the the javascript stuff. i was able to retrieve the categories fine, retrieve the subcategories as well. part of the problem was when the change() in jquery gets triggered, the get variable wasn't passing properly. i figured that part out, then i had the problem with looping through json. then i found my error, and now it's working.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community