Nishant Jani
Nishant Jani

Reputation: 1993

Dynamically populate dropdown from database

I am looking to do this :

I have two field 1. Date - using jquery datepicker 2. DropDown - Populated with student id

So , when i select a date say 03/09/2012 i should get all student id present on that day (say 15 of them) , then when i change the date to 04/09/2012 i should get id of only student that are present on that day ( say 29 of them)

I have done is , i have made ajax call to query the database like :

$('#date_selected').blur(function(){
$.ajax({
//query database and get the array of student id present on that date
//but from jquery how do i know populate my dropdown ?
//i would probably get the results json encoded
});
});

No issues about getting the data, i just want to know how to populate the dropdown using the array(json) that i have

Upvotes: 0

Views: 907

Answers (2)

Vipin Jain
Vipin Jain

Reputation: 1402

Use this inside your ajax success function:

data = $.parseJSON(data);

var select_fields = data.selectf;

var select_id = "/*ID OF SELECT FIELD*/";

$('#'+select_id+' option').remove();

for(var x  in select_fields){
    $('#'+select_id).append($('<option value="'+select_fields[x]['value']+'">'+select_fields[x]['name']+'</option>'));
}

Upvotes: 2

rahul
rahul

Reputation: 7663

you can use datepicker onselect method

 $('#date_selected').datepicker({
    onSelect:function (selectedDate) {
       //make your jquery ajax post here
    }
   });

Upvotes: 1

Related Questions