Reputation: 4579
I'm working on a code to dynamically set daysOfWeekDisabled in a dateTimePicker using Ajax.
I've got problem with the daysOfWeekDisabled's array. http://eonasdan.github.io/bootstrap-datetimepicker/Options/#daysofweekdisabled
Here is the code.
html:
<div id="show-pic" class="input-group bootstrap-timepicker">
<input id="datepicker" name="delivery_time" type="text" class="form-control input-small" placeholder="Choose a delivery time"/>
</div>
script:
$.ajax({
type: 'POST',
url: '../ajax.php',
success: function(result) {
console.log(result);
$('#datepicker').datetimepicker({
format: 'DD/MM/YYYY - H:mm',
minDate: new Date(),
daysOfWeekDisabled: [result]
});
}
});
ajax.php:
$week_day = array('0','6');
$string = implode(',', $week_day);
echo $string;
The console returns 0,6... great! but only Sunday is disable, 6 is ignored.
I tried several thing like "json_encode" or the "join()" function but can't get it.
Kindly please help.
Thank you.
Upvotes: 0
Views: 1195
Reputation: 4579
To solve the problem, I had to use "json_encode" (thank you @fauxserious).
So the code now.
script:
$.ajax({
dataType: 'json',
type: 'POST',
url: '../ajax.php',
success: function(result) {
console.log(result);
$('#datepicker').datetimepicker({
format: 'DD/MM/YYYY - H:mm',
minDate: new Date(),
daysOfWeekDisabled: result
});
}
});
ajax.php:
$week_day = array(0, 6);
echo(json_encode($week_day));
Thank you all!
Upvotes: 0
Reputation: 3940
Ok, I think I know what the problem is after asking some questions. I believe the php is returning a string back to your javascript, not an array (even though it might look like one). So in php use echo json_encode($week_day);
In the ajax response use JSON.parse(result)
to turn it back into an array. You might be able to set dataType: json
in the ajax call instead to have the result parsed when it comes back but I've had problems with that sometimes. If you console.log(result)
and you cannot twirl down the array as values (meaning it doesn't explicitly say Array[2]
with an arrow to twirl down) it's probably a string. Check out what you get with console.log(JSON.parse(result))
instead.
Upvotes: 1