I’m trying to insert dynamic values into the
datepicker()
method.
I can insert a single variable like so:
<html>
<head>
<?php $date='2-15-2013'; ?>
<script>
var date = '<?php echo $date; ?>';
/* create an array of days which need to be disabled */
var disabledDays = [
date,
"2-21-2013",
];
/* utility functions */
function bookedDays(date) {
var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
for (i = 0; i < disabledDays.length; i++) {
if($.inArray((m+1) + '-' + d + '-' + y, disabledDays) != -1 ) {
return [false];
}
}
return [true];
}
/* create datepicker */
jQuery(document).ready(function() {
jQuery('#datepicker').datepicker({
minDate: 0,
maxDate: "+3M +10D",
dateFormat: 'dd MM, yy',
constrainInput: true,
beforeShowDay: bookedDays
});
});
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker" /></p>
</body>
And the date ‘2-15-2013’ is duly disabled. Why can’t I then insert a string of values say:
$dates = ['2-21-2013', '2-24-2013', '2-27-2013'];
$unavailable = array();
foreach($dates as $date){
array_push( $unavailable, $date);
}
$unavaildays = implode(',', $unavailable);
$string = "{$unavaildays},";
?>
<script>
var dates = '<?php echo $string; ?>';
/* create an array of days which need to be disabled */
var disabledDays = [
dates
"2-21-2013",
];
/* utility functions */
function bookedDays(date) { ...
with dates:
2-21-2013,2-24-2013,2-27-2013,
becoming disabled? My values are parsed on the server at runtime aren’t they?
As @ahoo answered there is problem with quotation marks, but thats not all. It seems that
disabledDaysis also assigned in wrong way withdates.I would recommend something like that:
This way you have proper initial array made from php, and than you can add more dates in javascript.
Now, what was wrong:
When you added
datesthis way:Resulting array was
Note the first array value, it is not 3 dates, it is string glued from 3 dates.