0

I am returning a JSON array from my PHP class. Part of the code is as follows:

...
echo json_encode($users);
return json_encode($users);

?>

The echo statement of the above code is as follows:

[
    {
        "ID": "1",
        "firstname": "Jane",
        "school": "Frohj"
    },
    {
        "ID": "2",
        "firstname": "Mathew",
        "school": "Lebrasky"
    },
    {
        "ID": "3",
        "firstname": "BAHUUU",
        "school": "uni of kansas"
    }
]

Now, in my html form i have a DROPDOWN.

     <fieldset>
          <legend>Selecting elements</legend>
          <p>
             <label>Select list</label>
             <select id = "myList">
               <option value = "1">I WANT TO DISPLAY USER'S NAME HERE</option>
               <option value = "2">I WANT TO DISPLAY USER'S NAME HERE</option>

             </select>
          </p>
       </fieldset>

I want to display users firstnames as the fields in the dropdown box. The values for this, is obtained from the JSON that was passed. Now how do i, retrieve the JSON and populate the dropdown box with firstnames of users.

Illep
  • 16,375
  • 46
  • 171
  • 302

3 Answers3

0

You can decode to json result by json_decode($users); this function to convert json result it to array.

with the help of loop you can easily print your options.

Ranjeet Singh
  • 662
  • 5
  • 12
  • Where should i do this ? The main problem i have is to bring the result set to the HTML. how can i do this ? – Illep Oct 02 '14 at 10:04
  • what are you using to get result. ajax.? – Ranjeet Singh Oct 02 '14 at 10:07
  • Well, That is the problem i have. I am sending the JSON string, but i don't know how to retrieve the result. Can you help me here – Illep Oct 02 '14 at 10:08
  • so you need to parse json. like: success:function(result){ var text = parseJSON(result); } then use a for loop to create a tab dynamic in jquery file. and append to this your select box. Such as : for($i=0; $i'+text[i]['firstname']+'').appendTo('#myList'); } – Ranjeet Singh Oct 02 '14 at 10:12
  • with the help of loop you can easily print your options. parse JSON: success:function(result){ var text = parseJSON(result); $('#mylsit').empty(); for(var i=0; i'+text[i].firstname+'').appendTo('#mylist'); } } – Ranjeet Singh Oct 02 '14 at 10:22
0

Something like this:

head of the html page (with php extension)

<?php include 'pathTo/find.php' ?>

in your script tag:

var
  i, len,
  html = ''
  list = <?php echo getUsers(); ?>;

for ( i = 0; len = list.length; i < len ) {
    html += '<option value="'+ i +'">'+ list[i].firstname +'</option>';
}

// $( html ).appendTo( $( '#myList' ) ); <-- jQuery method
document.getElementById( 'myList' ).innerHtml = html;

your body tag:

<fieldset>
    <legend>Selecting elements</legend>
    <p>
    <label>Select list</label>
    <select id = "myList">
    </select>
    </p>
</fieldset>
TheGr8_Nik
  • 3,080
  • 4
  • 18
  • 33
0

Think your select should be empty at the beginning, then new options should be appended with javascript.

<select id = "myList"></select>

<script type="text/javascript">
var data = SOME_SORTOF_GET_JSON(), // get json from your existing function
    select = document.getElementById("myList"),
    newOption;

for(var i = 0, item; item = data[i]; ++i) {
    newOption = new Option(item.firstname, item.id);
    select.add(newOption);
}
</script>

Good luck.

yarmyarch
  • 19
  • 2