1

What i'm trying to do here to populate second select from based from the first one with json format.

Everything goes okay i'm getting the results i need with exact num of rows from the model based on the ID which i send with the request but i have this error :

http://img546.imageshack.us/img546/8895/ng1g.png "error"

I'm trying to get the mark id based on the model selected which i have in database :

ID MARK_ID VALUE_MARK

Here is my jquery function :

$(document).ready(function(){
$("#mark").change(function() {
   var markId= $("#mark").val();
   if (markId!= "") {
     $.get(
    'upload/car_models?mark_id=' + markId,
    function(data){                     
        $.each(data, function(key, value){
               $("#model").append("<option value='" + key + "'>" + value + "</option>");
        })
    },
    "json"
);
   }
});
})

And in my controller i'm doing json encode like this for the variable which is back from the model

echo json_encode($models);

How can i fix this problem with object Object. Thanks. Any help will be appreciate. EDIT : Json structure :

[{"id":"50","mark_id":"50","value_mark":"Refine","value":"JAC"},     {"id":"50","mark_id":"50","value_mark":"Rein","value":"JAC"}]

Here is my json structure. id mark_id value_mark here have 2 rows returned from the model

2
  • 1
    provide your json structure??? Commented Dec 27, 2013 at 16:11
  • [{"id":"50","mark_id":"50","value_mark":"Refine","value":"JAC"},{"id":"50","mark_id":"50","value_mark":"Rein","value":"JAC"}] Here is my json structure. id mark_id value_mark here have 2 rows returned from the model Commented Dec 27, 2013 at 16:12

2 Answers 2

1

Your data is an array of objects. WHen you loop over that array with $.each key will be index, and value will be object at that index.

Try:

$.each(data, function(i, obj){
      $("#model").append("<option value='" + obj.value + "'>" + obj.value_mark + "</option>");
 });

I guessed at what properties you want to assign to <option> value and text

Sign up to request clarification or add additional context in comments.

8 Comments

This works great. But when select ex. Audi all audi models are listed but then if i select Volvo the Audi models stays still there + the volvo models are added above. How can i fix this
before the each loop, remove old ones using $("#model").empty()
Thanks charlietfl this works great now. I will adjust just a little. But i need a little explanation about the i, obj in each data function what this means and how this fixed my problem. Thanks again
when looping an array first argument of $.each is index, second is the array element. Can name those arguments anything you want, I just used i to represent index and obj since the array element is an object, not a string or number etc
if you were looping an obbject with $.each , first argument would be the property key, second is value of that key
|
1

Try this,

$(document).ready(function(){
    $("#mark").change(function() {
        var markId= $("#mark").val();
        if (markId!= "") {
            $.get(
                'upload/car_models?mark_id=' + markId,
                function(data){   
                    var model=$("#model");
                    model.empty();
                    $.each(data, function(key, val){
                        model.append("<option value='" + val.value + "'>" + val.value_mark + "</option>");
                    });
                },
                "json"
                );
        }
    });
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.