从PHP和Mysql中禁用数据库中jquery datepicker中给定范围内的日期

时间:2019-03-06 11:09:33

标签: jquery-ui-datepicker

我在HTML和PHP中有此代码,问题是当我将beforeShowToday:checkAvailability放到Jquery代码中时,日期选择器出了错,日历无法显示,但是没有beforeShowToday,日历运行良好,请帮助!我对这个JQUERY非常了解,谢谢。

                                   <div class="form-group">
                                                <label>Check-In</label>
                                                <input name="date1" value="<?php echo $cin ?>" type="text" id="date1" class="form-control" required>

                                   </div>
                                   <div class="form-group">
                                                <label>Check-Out</label>
                                                <input name="date2" value="<?php echo $cout ?>" type="text" id="date2" class="form-control" required>

                                   </div>

这是我从预订表中获取日期的PHP代码

<?php
          //below function returns all the dates within a given range
     function date_range($first, $last, $step = '+1 day', $output_format = 'Y-m-d' ) {//if it was simple y example y-m-d then it will show 17 instead of 2017

   $dates = array();
   $current = strtotime($first);
   $last = strtotime($last);

    while( $current <= $last ) {

        $dates[] = date($output_format, $current);
        $current = strtotime($step, $current);
    }//end of while loop

        return $dates;//returns a array
       }//end of function
       //select all the  date ranges that exist in the database 
       $db = ligoanandb();
         $query = "SELECT check_in, check_out FROM reservation WHERE room_id = ?";

       $result = $db->prepare($query);
       $result->execute(array($room_id));

        $i = 0;
        while ($row=$result->fetch()){
        //output all the dates within the ranges in the database
        $range[$i] = date_range($row['check_in'], $row['check_out']);//creates a associative array with numerical index values



      $i++;     


      }

      $individual_dates = array();

     //converts the associative array  into a regular array
      foreach($range as $ranges){
         foreach ($ranges as $many_ranges){
           $individual_dates[] = $many_ranges;
         }
      }


     $json_array = json_encode($individual_dates);
?>

最后一个是JQUERY,当我将它放到ShowToday:checkAvailability时,datepicker不会显示,但是如果没有它运行良好,但是我需要那些检查可用性的文件,请帮助我,我非常喜欢。谢谢。

 <script>var date = new Date();
    $("#date1").datepicker({
        format: "yyyy-mm-dd",
        beforeShowDay:checkAvailability,
        todayBtn: true,
        autoclose: true,
        startDate: date
      })
      .on("changeDate", function(e) {
        var checkInDate = e.date, $checkOut = $("#date2");
        checkInDate.setDate(checkInDate.getDate() + 1);
        $checkOut.datepicker("setStartDate", checkInDate);
        $checkOut.datepicker("setDate", checkInDate).focus();
      });
    $("#date2").datepicker({
      format: "yyyy-mm-dd",
      todayBtn: true,
      autoclose: true,

      });
      /******************THE json array returned by php is used here*****/
              var $disabledDates = <?php echo $json_array; ?>

            function checkAvailability(date){
            var $return=true;
            var $returnclass ="unavailable";
            $checkdate = $.datepicker.format('yyyy-mm-dd',mydate);
            for(var i = 0; i < $disabledDates.length; i++)
                {    
                   if($disabledDates[i] == $checkdate)
                      {
                    $return = false;
                    $returnclass= "available";
                    }
                }
            return [$return,$returnclass];
            }
</script>

0 个答案:

没有答案