I'm trying to populate a select based on ajax data.
<input id="datepicker" name="date" width="270" style = "display: block; float: center;" required>
<script>
$('#datepicker').datepicker({
format: 'd/m/yyyy',
onSelect: function (date) {
$.ajax({
type: "POST",
url: '../../Models/appointment/timesquery.php',
data: {
'date': date,
},
}).done(function (data) {
$('select[name=times]').html(data);
}).fail(function () {
alert('An error has occured.')
});
}
});
</script>
<select class="select-css" name = "times">
<!-- Options will be added here dynamically using jQuery -->
</select>
timesquery.php
<?php
$date = $_POST['date']; // <====== Here is our date variable, you need to escape this!!!
$mysqli = NEW MySQLi('localhost','root','','cappeli');
$result = $mysqli->query("SELECT t.`hour` FROM `times` t LEFT JOIN ( SELECT `time` FROM `reservations` WHERE `date` = '$date' GROUP BY `time` HAVING COUNT(*) > 2) r on r.time = t.`hour` WHERE r.time IS NULL ");
while($rows = $result->fetch_assoc())
{
$hd = $rows['hour'];
echo "<option value = 'hairdressing'>$hd</option>";
}
?>
The select isnt populating, it staying null, and I replaced $date=$_POST['$date']; with $date='1/2/2020' just to make sure the query is correct, and when i opened the php file I got my list of values, but why isnt it populating in the select?