I am trying to populate a selected menu when the page is created but there are no options that show.
$(document).ready(function() {
$.ajax('patientlist.php', function(data){
var html = '';
var len = data.length;
for (var i = 0; i< len; i++) {
html += '<option value="' + data[i].patient_id + '">' + data[i].patient_firstname + data[i].patient_lastname + '</option>';}
$('#patientselect').append(html);
});
});
my patientlist.php
$result = mysql_query("SELECT `patient_id`, `patient_firstname`, `patient_lastname` FROM `patients` WHERE `company_id` = " . $user_data['company_id'] . " ORDER BY `patient_firstname`");
while($row = mysql_fetch_assoc($result)) {
$data[] = $row;
echo json_encode( $data );
}
My result from the php page
[{“patient_id”:”9″,”patient_firstname”:”Adam”,”patient_lastname”:”Steve”}] etc…
Really appreciate any help, been stuck on this for a week!
So, posting again.
First of all, you should put the
echo json_encode( $data );out of your while loopSecond, your
$ajaxsyntax isn’t correct, change this to$.postand tell the$.postrequest you are expecting a'json'response frompatientlist.phpWhen retrieving a valid
jsonstring, you can iterate overdataby using the$.eachmethodAt least you will now receive a valid request.
Noticing your HTML, do not use
.appendif it is not a DOM element, you are just building html elements as a string, so use instead