-1

I am trying to disable dates in datepicker that have been saved in mysql database and also deactivate all the past dates that one can't do booking on a past date.. I have the following code but its not working and even the datepicker can't pop out please help

Book.php

<?php
include("includes/db.php");  
if(isset($_GET['book'])){
    $id_book = $_GET['book'];
    $sql1="SELECT date,date_return,id 
            FROM booking 
            where id_book ='$id_book'";
    $query = $con->query($sql1);
    $dates_ar = [];
}   
if($query !== false && $searchResult->num_rows>0) {
    while ($r=$query->fetch_array()) {
        $begin = new DateTime( $r["date"] );
        $end = new DateTime( $r["return_date"] );
        $end = $end->modify( '+1 day' ); 
        $interval = new DateInterval('P1D');
        $daterange = new DatePeriod($begin, $interval ,$end);
        foreach ($daterange as $date) {
            $dates_ar[] = $date->format("Y-m-d");
        }
    }
}
?>

<input type="text" name="date" class="form-control" id="date">
<input type="text" name="return_date" class="form-control" id="return_date">


<script type="text/javascript">
$(function() {
    var disabledDays = <?php echo json_encode($date_arr)?>;
    var date = new Date();
    $( "#date").datetimepicker({ 
        dateFormat: 'Y-m-d',
        beforeShowDay: function(date) {
            var m = date.getMonth() + 1, 
            d = date.getDate(), 
            y = date.getFullYear(),
            strdate = [y,m,d].join('-');
            if (disabledDays.indexOf(strdate) == -1) {
                return [true, 'ui-state-active', ''];
            }
            return [false];
    }
});
});
</script>

reservation.php

<form action="book.php" method="post" name="a">
    <select  type="text" id="id_book"  name="id_book" />
        <option value"1">ID 1</option>
        <option value="2">ID 2</option>
    </select>
    <input type="submit" name="a"  value="val"/>
</form>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Hiram
  • 1
  • 4
  • Good code indentation would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Sep 07 '21 at 13:27
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Sep 07 '21 at 14:07
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 12 '21 at 13:01

1 Answers1

0

This code worked for me;

book.php

<?php

if(isset($_GET['book'])){
$book_id = $_GET['book'];
$sql1 = "select * from booking where equipment_id='$book_id'";
$query = $con->query($sql1);
$dates_ar = [];
}

if($query->num_rows>0) {
    while ($r=$query->fetch_array()) {
        $begin = new DateTime( $r["date"] );
        $end = new DateTime( $r["return_date"] );
        $end = $end->modify( '+1 day' ); 
        $interval = new DateInterval('P1D');
        $daterange = new DatePeriod($begin, $interval ,$end);
        foreach ($daterange as $date) {
        $dates_ar[] = $date->format("Y-m-d");
    }
}
}
?>

<script type="text/javascript"> 
$(function () {

    var disabledDate = <?php echo json_encode($dates_ar)?>;

    $('#date').datetimepicker({

    disabledDates: disabledDate,
    minDate:new Date()

    });

});

</script>
Hiram
  • 1
  • 4