Melih Erkin Sarıçam Melih Erkin Sarıçam - 1 year ago 167
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() {
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() {
target: '#check_result',
url: 'check.php',
success: function showResponse(responseText, $form) {
var picker = $input.pickatime('picker');
picker.set("disable", [
return false;

// PHP (check.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)) {
$row['time'] = date('H:i', strtotime($row['time']));
$row['time'] = str_replace(':', ',', $row['time']);
$disabled_times[] = '['.$row['time'].']';
/* free result set */
echo implode($disabled_times);

Answer Source

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

According to the docs ( 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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download