javascript - Populating Select with Jquery .ajax using json -
have tried hard no results json looks have created php (header sent before data)
{"users":[ {"id":"3256","name":"azad kashmir"}, {"id":"3257","name":"balochistan"}, {"id":"3258","name":"federally administered tribal areas"}, {"id":"3259","name":"islamabad"}, {"id":"3260","name":"north-west frontier"}, {"id":"3261","name":"northern areas"}, {"id":"3262","name":"punjab"}, {"id":"3263","name":"sindh"}], "results":8} and client side code this
<script> $(document).ready(function(){ $("#country").change(function(){ var vc = $("#country").val(); $.ajax({url: "../inc/region_json.php", type: "get", data:{country:vc}, datatype: 'json', success: function(json){ $("#divx").html(json); $.each(json, function(i, value) { $('#region').append($('<option>').text(value).attr('value', value)); }}); }); }); </script> i want populate
region: <select name="region" id="region"> <option value="0">choose country first</option> </select><br /><br />
try code:-
$(document).ready(function(){ $("#country").change(function(){ var vc = $("#country").val(); $.ajax({ url: "../inc/region_json.php", type: "get", data: { country: vc }, datatype: 'json', success: function (json) { $("#divx").html(json); $.each(json.users, function (i, value) { $('#region').append('<option value="'+value.id+'">' + value.name + '</option>'); }); } }); }); }); here sample demo well:- demo
Comments
Post a Comment