I have two combo boxes in an html form that are going to be populated dynamically from my database using jQuery and PHP. I call my jQuery when one of the combo boxes are changed and send the value of the changed combo box to my php page for it to look up values from my database.
function valueChanged(department){
$.get("AdminAjax.php",$("#department").serializeArray(),
function(data){
alert(data);//debug code that ouputs the returned value
var i =0;
for(i=0; i < data.length; i++){
//create a string that will be appended into the second combo box.
var option = '<option value="' + data.Category[i] +
'">=' + data.Category[i] + '</option>';
$("#category").append(option);
}
},"html" //debug for the return
);
}
I know that the value of the combo box is being passed over based on trial and error with the php page.
<?php
$department = mysql_real_escape_string($_GET["department"]);
//$department is the value passed from the jQuery.
$categorySQL = "Select Category from pagedetails where Department = '$department'";
//get the values for the second combo box based on the department
$rs = mysql_query($categorySQL);//<---this is where it fails.
//I have echoed out $rs and $rowCategory after I have fetched it.
//They return Resource #4 and Array respectively.
while($rowCategory = mysql_fetch_assoc($rs)){
//I am expecting multiple records to be returned.
$json_out = json_encode($rowCategory);
}
echo $json_out;
?>
your echo is wrong in your php is wrong and you need to use $.getJSON or $.ajax instead of
$.get. You are resetting the
$json_out` variable every time the while is true. You should save all the values to an array and then json_encode it once. This will also ensure that your json in the success is valid json.try this: