javascript 将数据库表中的日期提取到数组中以禁用特定/选定的日期选取器

cpjpxq1n  于 2023-01-11  发布在  Java
关注(0)|答案(1)|浏览(121)

I have table from database here, where I want to fetch the date only (fld_booking_date in tbl_booking database here), in order to disable particular dates here that have been selected in database.
这是我为日期设置的代码:

<div class="form-group">
                    <label for="BookingDate">Date</label>
                    <input class="form-control datepicker" name="fld_booking_date" id="fld_booking_date" placeholder="mm/dd/yyyy" value="" required>
                  </div>

这是我用来禁用日期选择的脚本,基于https://dzone.com/articles/disable-dates-in-datepickerhttp://jsfiddle.net/sibeeshvenu/gj90f1bm/

var array = ["2023-01-04","2023-01-05","2023-01-09", "2023-01-10", "2023-01-11", "2023-01-12", "2023-01-13"]

$('input.datepicker').datepicker({
    beforeShowDay: function(date){
        var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
        return [ array.indexOf(string) == -1 ]
    }
});

我想从数据库中提取日期作为数组,这样的日期可以禁用选择(使用数据库中已经存在的日期,而不是这7个日期)。我完全是新的,所以我希望任何人都可以有点办法得到数组从数据库中提取日期。谢谢你这么多,完全赞赏~

hi3rlvi2

hi3rlvi21#

真主!
首先,你必须把所有的日期保存在数据库中(PHP代码),如:

$dates = array();
        
$date_query = "SELECT `YOUR_TABLE`.`YOUR_DATE_COLUMN` FROM `YOUR_TABLE`
                where YOUR_CONDITIONS";
$date_result = mysqli_query($con, $date_query) or die("Query Unsuccessful.");
  if (mysqli_num_rows($date_result ) > 0) {
    while ($row = mysqli_fetch_assoc($date_result )) {
       $dates [] = $row['YOUR_DATE_COLUMN'];
    }
  }

然后在javascript变量中回显此变量,如下所示:

var array = <?= json_encode($dates)?>;
$('input.datepicker').datepicker({
   beforeShowDay: function(date){
      var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
       return [ array.indexOf(string) == -1 ]
   }
});

真主阿拉!祝你有美好的一天。

相关问题