Melih Erkin Sarıçam Melih Erkin Sarıçam - 4 months ago 43
Javascript Question

Updating Pickadate Input via Ajax

I use Pickadate.js and JQuery Form Plugin. I have date and time pickers seperately. What I want to do is to disable times in timepicker according to value of datepicker. So, I am trying to get the JSON data into

picker.set("disable", [ ]);
. I can console.log the plain text but it remains aimless.

I tried a lot and have come across these solutions. But I couldn't launch them. (I adapted pickadate functions and classes to pickatime's.)



// Javascript
$(document).ready(function() {
$("input").click(function() {
$(".datepicker").pickadate({
format: 'yyyy-mm-dd',
formatSubmit: 'yyyy-mm-dd',
min: true,
max: false
});
var $input = $(".timepicker").pickatime({
format: 'HH:i',
formatSubmit: 'HH:i',
formatLabel: 'HH:i'
});
$('.datepicker').change(function() {
$('#form').ajaxSubmit({
target: '#check_result',
url: 'check.php',
success: function showResponse(responseText, $form) {
var picker = $input.pickatime('picker');
picker.set("disable", [
console.log(responseText)
]);
}
});
return false;
});
});
});

// PHP (check.php)
<?php
require ('connect.php');
$date = mysql_real_escape_string($_POST['date']);
$match_query = mysql_query("SELECT * FROM booking where DATE(time)='$date'");
$disabled_times = array();
if ($result = $match_query) {
/* fetch associative array */
while ($row = mysql_fetch_assoc($result)) {
setlocale(LC_ALL,"tr_TR.UTF-8");
$row['time'] = date('H:i', strtotime($row['time']));
$row['time'] = str_replace(':', ',', $row['time']);
$disabled_times[] = '['.$row['time'].']';
}
/* free result set */
mysql_free_result($result);
}
echo implode($disabled_times);
?>




Answer

Can you post an example of the json being returned from your php?

According to the docs (http://amsul.ca/pickadate.js/api/#method-get-disable) your json should be something like this: [2,30], [4,30], [9,0]

If your json is correct, be sure it is not being passed to the timepicker as a string. Try something like:

var json = JSON.parse(responseText);
picker.set("disable", [ json ]);

UPDATE: I guess with the following code, your json will return properly:

...
$time = explode(',', $row['time']);
$time[0] = (int)$time[0];
$time[1] = (int)$time[1];
$disabled_times[] = $time;
...
echo json_encode($disabled_times);
Comments