i have one getJSON request which is returning proper values but its not adding in list
here is code
<script type="text/javascript">
$(document).ready(function() {
$("select#make").change(function(){
$('div.data_loading').fadeIn();
$.getJSON("getmod.php",{id: $(this).val(), ajax: 'true'}, function(j){
var options = '';
for (var i = 0; i < j.length; i++) {
options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
}
$("select#vmod").html(options);
$('div.data_loading').hide();
});
})
})
</script>
Bellow is values returned from getmod.php
[{optionValue:13, optionDisplay:156},{optionValue:14, optionDisplay:164},{optionValue:15, optionDisplay:166},{optionValue:16, optionDisplay:GTV-6},{optionValue:17, optionDisplay:Spark},{optionValue:18, optionDisplay:Spider},{optionValue:19, optionDisplay:Graduate},{optionValue:20, optionDisplay:Quadrifoglio},{optionValue:21, optionDisplay:Milano},]
here is select box
<select name="vmod" id="vmod" >
</select>
bellow is my php file which return JSON
$id=$_GET['id'];
$sql="select mname,mod from modl where mkd=".$id;
$result=mysql_query($sql);
echo "[";
for($i=1;$i<=mysql_num_rows($result);$i++)
{
$row=mysql_fetch_array($result);
echo "{optionValue:".$row['mod'].", optionDisplay:". $row['mname']."}";
echo ",";
}
echo "]";
Your JavaScript code looks ok.
Is this the exact response form the server? Then your JSON is not valid. String values and keys have to be enclosed in double quotes (I’m also not sure about the trailing comma). Paste your JSON here and test it. Also have a look at http://json.org/.
This would be valid:
It pretty much looks like you built the JSON string manually (using string concatenation and a loop). Don’t do that, use
json_encodeinstead.Update: Here is how it should be:
Also note that your code is prone to SQL injection attacks. You should use
mysql_real_escape_stringon$_GET['id']. Or better, use prepared statements.