简体   繁体   中英

datepicker disable dates before today

i know there a lot of similar question like this but i made mine because i cant find my answer in there questions. What i want to achieve is to prevent user to select later days. ex. its 8/7/2017 today the user must cannot select 8/6/2017.

i try this code but i dont know why its not working <script>$('#date').datepicker({ minDate: 0 });</script>

this is my codes

<!DOCTYPE html>

<?php
    error_reporting(E_ALL & ~E_NOTICE);
    error_reporting(E_ERROR | E_PARSE);
    session_start();
?>

<html>
<head>
<title>Reservation</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>

<nav class="clearfix">
        <ul class="clearfix">
            <li><a href="index.php"><img src="img\home.png" style="margin-bottom:-2px; margin-right:3px; width:16px; height:16px;">Home</a></li>
            <li><a href="speaker.php"><img src="img\speakers.png" style="margin-bottom:-1px; margin-right:4px; width:15px; height:13px;">Speakers</a></li>
            <li><a href="about.php"><img src="img\about.png" style="margin-bottom:-1px; margin-right:3px; width:13px; height:12px;">About</a></li>
            <li><a href="contact.php"><img src="img\contact.png" style="margin-bottom:-2px; margin-right:6px; width:13px; height:14px;">Contact</a></li>
            <li><a href="reservation.php"><img src="img\reservation.png" style="margin-bottom:-2px; margin-right:5px; width:14px; height:13px;">Reservation</a></li>
            <li><a href="signOut.php" id="signOut"><img src="img\signOut.png" style="margin-bottom:-2px; margin-right:6px; width:14px; height:14px;">Sign Out</a></li>
            <li><a href="myAccount.php" id="user" style="text-transform:capitalize;"><img src="img\user.png" style="margin-bottom:-1px; margin-right:6px; width:13px; height:12px;"><?php echo $_SESSION['firstname']; ?></a></li>
            <li><a href="signUp.php" id="signUp"><img src="img\signUp.png" style="margin-bottom:-1px; margin-right:6px; width:13px; height:11px;">Sign Up</a></li>
            <li><a href="signIn.php" id="signIn"><img src="img\signIn.png" style="margin-bottom:-2px; margin-right:6px; width:14px; height:13px;">Sign In</a></li>
            <li><a href="adminControl.php" id="adminControl"><img src="img\admin.png" style="margin-bottom:-3px; margin-right:6px; width:15px; height:16px;">Admin control</a></li>
        </ul>
        <a href="#" id="pull">Speaker Reservation</a>
</nav>

<div id="register-wrapper">

    <div id="register-container">
    <h2>Reservation</h2>
    <p>About the Event</p>
    <hr>
    <div id="alertCaptcha">
        <span onclick="this.parentElement.style.display='none';">&times;</span>
        Please check the reCaptcha.
    </div>
     <div id="reservationSuccess">
        <span onclick="this.parentElement.style.display='none';">&times;</span>
        Reservation partially complete. Please check your email to see the bankcode and deposit the downpayment.
    </div>
    <form id="reservationform" name="reservationform" method="POST">
    <input name="speaker" class="inputbox" type="text" autofocus required="required" maxlength="20" value="<?php echo $_SESSION['speaker'] ?>" placeholder="Who is the speaker that you prefer?">
    <br>
    <input name="topic" class="inputbox" type="text" autofocus required="required" maxlength="50" value="<?php echo $_SESSION['gettopic'] ?>" placeholder="What is the topic of the event?">
    <br>
    <input name="location" class="inputbox" type="text" autofocus required="required" maxlength="50" value="<?php echo $_SESSION['getlocation'] ?>" placeholder="What is the complete address of the venue?">
    <br>
    <input name="audience" class="inputbox" type="text" autofocus required="required" maxlength="50" value="<?php echo $_SESSION['getaudience'] ?>" placeholder="Who are coming?">
    <br>    
    <input name="count" class="inputbox" type="text" autofocus required="required" maxlength="50" value="<?php echo $_SESSION['getcount'] ?>" placeholder="How many people are coming?" onkeypress="return isNumberKey(event)">
    <br>
    <input name="time" class="inputbox" type="text" autofocus required="required" maxlength="50" value="<?php echo $_SESSION['gettime'] ?>" placeholder="What time is the start and end of the event?">    
    <br>
    <input id="date" name="date" class="inputbox" type="date" autofocus required="required" maxlength="50" value="<?php echo $_SESSION['getdate'] ?>" placeholder="When is the exact date?"> 
    <br>
    <br>
    <div class="g-recaptcha" data-sitekey="6LfHDikUAAAAAB0IsgeTrT1J_B8dBQlf-RMrLMaq"></div>
    <input name="register"  id="buttonsubmit" type="submit" value="Register">
    </form> 
    <br>
</div>
<div id="footer">Copyright 2017</div>

<script>
$(function() 
    {
        var pull = $('#pull');
        menu = $('nav ul');
        menuHeight  = menu.height();
        $(pull).on('click', function(e) 
        {
            e.preventDefault();
            menu.slideToggle();
        });

        $(window).resize(function()
        {
            var w = $(window).width();
            if(w > 320 && menu.is(':hidden')) 
            {
                menu.removeAttr('style');
            }
        });
    });
</script>

<script>
    function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
      }

</script>

<script>
    function reservationSuccess() 
    { 
       document.getElementById("reservationSuccess").style.display = "block";
    }
</script>

<script>
    function alertCaptcha() 
    { 
       document.getElementById("alertCaptcha").style.display = "block";
    }
</script>

<script>
function ifAdmin() 
{ 
   document.getElementById("signIn").style.display = "none";
   document.getElementById("signUp").style.display = "none";
   document.getElementById("signOut").style.display = "block";
   document.getElementById("adminControl").style.display = "block";
}
</script>

<script>
function ifNotAdmin() 
{ 
   document.getElementById("signIn").style.display = "none";
   document.getElementById("signUp").style.display = "none";
   document.getElementById("signOut").style.display = "block";
   document.getElementById("adminControl").style.display = "none";
}
</script>

<script>
function ifNotLogin() 
{ 
   document.getElementById("user").style.display = "none";
   document.getElementById("signOut").style.display = "none";
   document.getElementById("adminControl").style.display = "none";
}
</script>

<script>
$('#date').datepicker({ minDate: 0 });
</script>

<?php
  function email($to, $subject, $body)
  {
     mail($to, $subject, $body, 'From: SpeakerReservation');
  }

?>



<?php

    if (isset($_SESSION['signedIn']) && $_SESSION['signedIn'] == true) 
        //if login
        {
            if($_SESSION['type'] == 1)
            {
                echo "<script type='text/javascript'>ifAdmin();</script>";  
            }
            elseif($_SESSION['type'] == 0)
            {
                echo "<script type='text/javascript'>ifNotAdmin();</script>";
            }
        }
        //if not login
        else
        {
            echo "<script type='text/javascript'>ifNotLogin();</script>";   
        }
?>

<?php

if(isset($_POST["register"]))
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "srdatabase";

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) 
{
    die("Connection failed: " . $conn->connect_error);
} 
    date_default_timezone_set('Asia/Manila');
    $reservationDate = date("F j, Y");
    $reservationTime = date("g:i a");

    $getfirstname = $_SESSION['firstname'];
    $getlastname = $_SESSION['lastname'];
    $getemail = $_SESSION['email'];
    $getcontactnumber = $_SESSION['contactnumber'];
    $emailcode = md5($_POST["firstname"] + microtime());
    $secretKey = "6LfHDikUAAAAAJyLG9Op1gRi81iPUaxXnIZQWZ2c";
    $responseKey = $_POST['g-recaptcha-response'];
    $userIP = $_SERVER['REMOTE_ADDR'];
    $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";
    $response = file_get_contents($url);
    $response = json_decode($response);

    $insertdata = "INSERT INTO reservations (firstname, lastname, emailaddress, contactnumber, speaker, topic, location, audience, count, time, date, reservationdate, reservationtime)VALUES('$getfirstname', '$getlastname', '$getemail', '$getcontactnumber', '".$_POST["speaker"]."', '".$_POST["topic"]."', '".$_POST["location"]."', '".$_POST["audience"]."', '".$_POST["count"]."', '".$_POST["time"]."', '".$_POST["date"]."', '$reservationDate', '$reservationTime')";

    if($response->success)
    {        
        mysqli_query($conn, $insertdata);
        echo "<script type='text/javascript'>reservationSuccess();</script>";  
        email($getemail, 'Activate your account',"Hello ".$getfirstname.",\n\nUse the link below to activate your account\n\nhttp://zdadental.net16.net/Activate.php?email=" . $getemail . "&email_code=" . $emailcode ."\n\n-SpeakerReservation");                             
        unset($_SESSION['gettopic']);
        unset($_SESSION['getlocation']);
        unset($_SESSION['getaudience']);
        unset($_SESSION['getcount']);
        unset($_SESSION['time']);
        unset($_SESSION['getdate']);
    }
    else
    {
        echo "<script type='text/javascript'>alertCaptcha();</script>";  
        $_SESSION['gettopic'] = $_POST['topic'];
        $_SESSION['getlocation'] = $_POST['location'];
        $_SESSION['getaudience'] = $_POST['audience'];
        $_SESSION['getcount'] = $_POST['count'];
        $_SESSION['gettime'] = $_POST['time'];
        $_SESSION['getdate'] = $_POST['date'];
    }

$conn->close();
}
?>

</body>
</html>

Use this code:

You must create a new date object and set it as minDate when you initialize the datepickers

var dateToday = new Date();
var dates = $("#date").datepicker({
    defaultDate: "+1w",
    minDate: dateToday,
    onSelect: function(selectedDate) {

    }
});

Example:

 $(window).load(function(){ var dateToday = new Date(); var dates = $("#from, #to").datepicker({ defaultDate: "+1w", minDate: dateToday }); }); 
 <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.js"></script> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/i18n/jquery-ui.min.js"></script> <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css" type="text/css" media="all"> <label for="from">From</label> <input type="text" id="from" name="from"/> <label for="to">to</label> <input type="text" id="to" name="to"/> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM