1

I have an array

{
    "status": "OK",
    "message": "Data Show Successful",

"allknife": [
        {
            "name": "Folder",
            "notes": ""
        },
        {
            "name": "Folder2",
            "notes": ""
        }
        }
    ]
}

My current code run correctly

my current code

<script>
$(document).ready(function() {

    $("#person").change(function () {
       var val = $(this).val()
        if(val == "") return
       $.getJSON("http://some.com/Knife/api/l.php?&returnformat=json", {"state":val}, function(res,code) {





          $("#name").val(res.message)
            $("#age").val(res.status)
       })
   })

})
</script>

now i want to populate my select box value from allknife array every array name.

like

<option value="folder">0</option>
<option value="folder2">1</option>

here value 0 and ones means folder is 1st array o allknife

when user select #person select box then other fields of form fill with mycurrent code then when user select second select box 0 then other fields fill.

if they select folder then other filed fill from allknife o no array. if folder2 then other field fill from allknife array 2

my form looks like

<form>
<select name="person" id="person">

<input type="text" name="name" id="name"><br/>
<input type="text" name="age" id="age"><br/>


<select id="allknife"></select>


<input type="text" name="name" id="name"><br/>
<input type="text" name="note" id="note"><br/>
<input type="text" name="codskill" id="codskill"><br/>
</form>
  • http://stackoverflow.com/questions/1502649/jquery-getjson-populate-select-menu-question – R D Jun 05 '14 at 06:59

1 Answers1

0

You need to loop over your returned array. Something like this:

$.getJSON("http://some.com/Knife/api/l.php?&returnformat=json", { "state": val }, function(res, code) {
    $("#name").val(res.message);
    $("#age").val(res.status);
    $("#codskill").val(res.allknife[0]['name']);

    $.each(res.allknife, function(i, item) {
        $('<option />', { text: i, value: item.name }).appendTo('#allknife');
    });
})

Example fiddle

Also, your <select name="person" id="person"> is missing a closing tag.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339