Andrew Linton
Andrew Linton

Reputation: 1

disable days in datepicker with mysql

I have been battling with this jquery mobile datepicker for 2 days now!! 

here is my code, i am retrieving dates with a boolean set to 1, (disabled days), i cannot get datepicker to disable them!! please help.. I am trying to return an array with json to the datepicker from my mysql query

booked_dates.php contents:

<?php
require_once("connect.php");
mysql_select_db("eyecandysf");
$booked_date = mysql_query("SELECT date, FROM date_time WHERE booking_status !=0  ");

 while ($result = mysql_fetch_array($booked_date)){

    $dates[] =  $result['date'];
}
echo json_encode($dates);


?>

main.php contents:

<html>
<head>

<link rel="stylesheet" type="text/css"       href="http://code.jquery.com/mobile/latest/jquery.mobile.min.css" />
<link rel="stylesheet" type="text/css" href="http://dev.jtsage.com/cdn/datebox/latest/jqm-datebox.min.css" /> 
<link rel="stylesheet" href="http://mobile-bydesign.com/mmc/mobile/3.0.4/themes/eyecandysf22.css" />

<link href="http://mobile-bydesign.com/mmc/mobile/3.0.4/photoswipe.css" type="text/css" rel="stylesheet" />

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script> 
<script type="text/javascript" src="http://code.jquery.com/mobile/latest/jquery.mobile.min.js"></script>
<script type="text/javascript" src="http://dev.jtsage.com/cdn/datebox/latest/jqm-datebox.core.min.js"></script>
<script type="text/javascript" src="http://dev.jtsage.com/cdn/datebox/latest/jqm-datebox.mode.calbox.min.js"></script>
<script type="text/javascript" src="datepicker_mobile.js"></script>
<script type="text/javascript">
   $.getJSON('booked_dates.php', function(data) {
     var bookedDays = data;
    });

  function isAvailable(date){
   var dateAsString = date.getFullYear().toString() + "-" + (date.getMonth()+1).toString() + "-" + date.getDate();
   var result = $.inArray( dateAsString, bookedDays ) ==-1 ? [true] : [false];
    return result
    }

    $('#date').datepicker({minDate: 0, maxDate: "+2M", beforeShowDay: isAvailable});

   </script>
<style>
 .ui-select .ui-btn select{
   font-size: 50px;
   }

</style>

</head>


<body>

<form data-ajax="false" id="date" action="time.php" method="post">

<div id="dateDiv" data-role="fieldcontain">    
    <label for="mydate" class="ui-hidden-accessible">date </label>
    <input  name="date" type="date" id="date" data-theme="b" data-role="datebox" value="" placeholder="date"
      data-options='{"mode": "calbox", "afterToday": true,"blackDays": [1], "maxDays": 65}'/>
        </div>
  <input type="submit" value="select time" name="submit" />
  </form>
  </body>
  </html>

Upvotes: 0

Views: 2093

Answers (1)

poncha
poncha

Reputation: 7866

The problem with your code is that bookedDays variable is defined only inside ajax callback (it is a local variable). And it is even undefined in global scope completely.

So:

var bookedDays = [];

$(document).ready(function(){
   $.getJSON('booked_dates.php', function(data) {
     bookedDays = data;
     });
});

This will both ensure you're working with a global variable, and prevent errors accessing it before the ajax call finished.

EDIT: Additionally, to avoid problems with date formatting, use ISO date on javascript side as well:

var dateAsString = date.toISOString().substring(0,10);

Upvotes: 1

Related Questions