tags:

views:

37

answers:

1

I am using following code to populate DropDown:

$(document).ready(function(){
    $('#inDistrict').sSelect();
    $("#inDistrict").change(function(){
    $.getJSON("filldistricts.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>';
      }
    })
  })
})

The code for the file: filldistricts.php is as below:

<?php
    require_once("../Lib/dbaccess.php");
    $query = "SELECT districtid, districtname FROM districtmaster";
    try
    {
    $result = dbaccess::GetRows($query);
    echo json_encode($result);
    }
    catch(exception $ex)
    {
        echo "<script type='text/javascript'>alert('".$ex."')</script>";
    }
?>

The DropDown is not filling. Where is the problem?

Edited:

DBAccess.php [GetRows function] contains only following code:

$resultSet = mysql_query($inQuery);
return $resultSet;  

Connection is opened before the above code.

+1  A: 

You're creating the options variable in the change handler but you aren't actually putting the end result into the dropdown at all!

Try:

$.getJSON("filldistricts.php",{id: $(this).val(), ajax: 'true'}, function(j){
  var options = [];
  for (var i = 0; i < j.length; i++) {
    options.push( '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>' );
  }
  // change #whatever to the id of the dropdown you
  // want to update, it's not clear from your question
  $('#whatever').html( options.join('') );
})
thenduks
FYI, `push` and then `join` instead of string concatention should be quite a bit faster, that's why I changed it.
thenduks
Still not working.
RPK
Can you be more specific? Does the result from the back-end come back correctly? Try `console.log( options.join('') )` after the for loop and make sure it looks right. What dropdown are you trying to modify?
thenduks