views:

42

answers:

3

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"&gt;&lt;/script&gt;
<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"&gt;&lt;/script&gt;
    <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> 
A: 

One problem I see is that in your each loop, you're just assigning j.options to myarray instead of adding it to the array. Change to:

myarray.push(j.options);
mbrevoort
A: 

i'm not sure what your json data looks like, but maybe you need something like this:

function Preload() {
  $.getJSON("http://localhost/conn_mysql.php", function(jsonData){  
    $.each(jsonData, function(i,j){ 
      $('#ID-OF-YOUR-SELECT-ELEMENT').append(
        $('<option></option>').val(j.value).html(j.text)
      );
    }
  );
});}

j.value = the value of the option you want to add j.text = the name of the option you want to add (what the user sees)

LAX
it looks like [{"options":"smart_exp"},{"options":"user_intf"},{"options":"blahblah"}]
XCeptable
A: 

First of all, .getJSON() uses a callback, meaning that the code executed as the second argument of .getJSON() won't necessarily have been called by the time your for loop runs. It runs once the server responds with your conn_mysql.php JSON data. Try nesting it:

function Preload() {
    $.getJSON("http://localhost/conn_mysql.php", function (jsonData) {
        $.each(jsonData, function (i, j) {
            document.form1.fruits.options[i] = new Option(j.options);
        });
    });
}

You should be aware of the structure of your jsonData. Here, this would only work if the structure were:

[
    { options: "item 1" },
    { options: "item 2" },
    { options: "item 3" },
]

It loops through the entire object, finding each element of the array, then looks for that element's options property. Is that what you're looking for?

clarkf
Grate, u r grate ...... thanX a Lot. Its working now with ur code. One problem is it works only in IE so far, do not work in firefox. I think I need to make //$('document').ready(function() working that is commented now in my this code as I was not able to fix it. Can u see it, I think it has only bracees problem. Can u kindly tell me one more thing, can PURE i.e. a JS rendering engine be used in such case. Provided I have no info so far about JS rendering engines. thanX a thousand again clarkf :-) –
XCeptable
@babar - I'm not sure what you're asking. If you're asking about the pure templating library (http://beebole.com/pure/), sure, it could be used here. You'd need to modify your JSON data a little in the PHP script (I think, I'm unfamiliar with pure). Check out the documentation?
clarkf