I have developed a script that receives json data from a php script using $.getJSON. The JSON data looks like '[{"options":"smart_exp"},{"options":"user_intf"},{"options":"blahblah"}] '
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
//$('document').ready(function() {
function Preload() {
$.getJSON("http://localhost/conn_mysql.php", function(jsonData){
$.each(jsonData, function(i,j)
{ alert(j.options);});
});}
// });
</script></head>
<body onLoad="Preload()">
</body>
</html>
I also developed a script that dynamically generates a dropdown list using an array.
<HTML>
<HEAD>
<script language="javascript">
var myarray = new Array("apples","oranges","bananas","Peac");
function populate()
{ for (i=0; i<myarray.length; i++)
{
document.form1.fruits.options[i]=new Option(myarray[i], i);
}
}
</script>
</HEAD>
<body onload="populate();">
<form name="form1">
My favourite fruit is :
<select name="fruits" />
</form>
</body>
</HTML>
Now I need to dynamically build a dropdown list using data returned by getJson but I am having trouble in merging both. I will be very thankful for the help. Here is what I tried to do but its not working.
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
//$('document').ready(function() {
function Preload() {
var myarray = new Array();
$.getJSON("http://localhost/conn_mysql.php", function(jsonData){
$.each(jsonData, function(i,j)
{ myarray = j.options;});});
for (i=0; i<myarray.length; i++)
{ document.form1.fruits.options[i]=new Option(myarray[i]); }
}
// });
</script></head>
<body onLoad="Preload()">
<form name="form1">
My favourite fruit is :
<select name="fruits" />
</form>
</body>
</html>