I have a php script that queries my database for some values and puts them into a json string.
<?php
if (isset($_POST['name']) === true && empty($_POST['name']) === false) {
include_once("../php_scripts/connect.php");
$arr = array();
if ($result = $mysqli->query("SELECT due_date FROM payment_due_dates WHERE client_id = '" . $_POST['name']. "'")) {
$rowCount = $result->num_rows;
echo '[';
$i = 0;
while ($row = $result->fetch_assoc()) {
if ($i <= $rowCount - 2){
echo json_encode($row);
echo ',';
}else{
echo json_encode($row);
}
$i++;
}
$result->free();
}
echo ']';
}
?>
Then jquery converts into a json object and adds the values to a select box.
$(document).ready(function(){
$('#subName').click(function(){
var name = $('input#txtName').val();
if($.trim(name) != ''){
$.post('ajax/load_patient_episodes.php', {name: name}, function(data){
console.log(data);
var obj = $.parseJSON(data);
console.log(obj);
var output;
$.each(obj, function(key, val){
output += '<option>' + val.due_date + '</option>';
});
$('#selEpisode2').html("<option>" + output + "</option>");
});
}
});
});
It loads the values but also gives this error in my console log:
event.returnValue is deprecated. Please use the standard event.preventDefault()
instead. jquery.js:5
and, when it adds the values to the selectbox it also adds the text 'undefined'. So the options in my select box appear as:
undefined
2013-11-13
2013-11-13
2013-11-13
I'm assuming it may have something to do with the proto array that is in there but I'm not sure how it got there. Still somewhat a newbie on this stuff. Here is my console log. Any ideas how I can lose the 'undefined in my select options?' Also, is there a better way I should be going about loading select options from a php query?
event.returnValue is deprecated. Please use the standard event.preventDefault()
instead. jquery.js:5
[Object, Object, Object]
0: Object
1: Object
2: Object
length: 3
__proto__: Array[0]