简体   繁体   中英

how can i validate the date for my reservation?

I made a reservation form, and I need a date validation.

If the date is less than the current date, it should show an alert message saying "Please select a higher date" the stay on the page to change the date. But there is problem in my alert: when I choose a higher date, it still shows the alert message.

Here is my code:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="tcal.css" />
<link rel="stylesheet" type="text/css" href="style.css" />
<link href="bootstrap/css/bootstrap.css" rel="stylesheet"/>
<script type="text/javascript" src="tcal.js"></script>
<script>
function showUser(str) {
  if (str=="") {
    document.getElementById("txtHint").innerHTML="";
    return;
  } 
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","getuser.php?q="+str,true);
  xmlhttp.send();
}

</script>

<script>
  function showCottage(str) {
  if (str=="") {
    document.getElementById("txtcot").innerHTML="";
    return;
  } 
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      document.getElementById("txtcot").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","getcottage.php?q="+str,true);
  xmlhttp.send();
}
</script>
</head>
<body>

<?php
session_start();
$message=''; //error message
$reserve=@$_POST['reserve'];
$dayCount = date("d");
$year = date("Y");
$month = date("m");
$currentDate = $year.'-'.$month.'-'.$dayCount;

if ($reserve <= $currentDate){
  ?> 
    <script type="text/javascript">
    function myFunction(){
      alert("Please select a higher date!"); 
      return false;
      //history.back();
    }
    </script> 
  <?php 
  }

 ?>



<form action="all.php" method="post" onsubmit="return myFunction()">

<div class="container">
<div id='fg_contact'>
<fieldset>
<legend>Contact Details</legend>

<input type="text" name="name" class="txt" placeholder="Full Name:" /><br/>
<input type="text" name="address" class="txt" placeholder="Address:" /><br/>
<input type="text" name="contact" class="txt" placeholder="Contact Number:" /><br/>
<input type="text" name="email" class="txt" placeholder="Email Address:" /><br/>
</fieldset>
</div>
<br/>

<div id='fg_reservation'>
<fieldset>
<legend>Reservation Details</legend>

<p>
        <label for="password">Date</label><br/>
        <input name="reserve" type="text" class="tcal txt-reg" id="fgh"/>
</p>
<br/><br/>

<table class="table-res">
<td class="table-td">

<select class="txt-reg" name="users" onchange="showUser(this.value)">
<option >Select a time:</option>
<option value="1">DayTime</option>
<option value="2">NightTime</option>
<option value="3">OverNight</option>
</select>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
<br/>


<select class="txt-reg" name="Cot" onchange="showCottage(this.value)">
<option >Select a cottage:</option>
<option value="4">Cottage1</option>
<option value="5">Cottage2</option>
<option value="6">Cottage3</option>
<option value="7">Cottage4</option>
<option value="8">Cottage5</option>
<option value="9">Cottage6</option>

<option value="10">Cottage7</option>
<option value="11">Cottage8</option>
<option value="12">Cottage9</option>
<option value="13">Cottage10</option>
<option value="14">Cottage11</option>
<option value="15">Cottage12</option>

<option value="16">Cottage14</option>
<option value="17">Cottage15</option>
<option value="18">Cottage16</option>
<option value="19">Cottage17</option>
<option value="20">Cottage18</option>
<option value="21">Cottage19</option>

<option value="22">Cottage20</option>
<option value="23">Cottage21</option>
<option value="24">Cottage22</option>
<option value="25">Cottage23</option>
<option value="26">Cottage24</option>
<option value="27">Cottage25</option>

<option value="28">Cottage26</option>
<option value="29">Cottage27</option>
<option value="30">Cottage28</option>
<option value="31">Cottage29</option>
<option value="32">Cottage30</option>
<option value="33">Cottage31</option>

<option value="34">Cottage32</option>
<option value="35">Cottage33</option>
<option value="36">Cottage34</option>
<option value="37">Cottage35</option>
<option value="38">Cottage36</option>
<option value="39">Cottage37</option>

<option value="40">Cottage38</option>
<option value="41">Cottage39</option>
<option value="42">Cottage40</option>
<option value="43">Cottage41</option>
</select>

<div id="txtcot"><b>Price of cottage will show here.</b></div>
<br/></td>

<td class="table-td">
Adult's Head Count :<br/>
<input type="text" class="txt-reg" name="headcount1" value="0" /><br/>
Children's Head Count :<br/>
<input type="text" class="txt-reg" name="headcount2" value="0" /><br/><br/>

<input type="submit" value="Calculate Discount" />
</td>
</table>
</fieldset>
</div>
</div>
</form>



</body>
</html>

Use strtotime() function like below:

if (strtotime($reserve) <= strtotime($currentDate))

instead of

if ($reserve <= $currentDate)

Your Javascript myFunction() is between 2 blocks of PHP and not inside PHP. Then it loads unconditionnaly.

You have to structure your code differently. onsubmit="return myFunction()" could become onsubmit=<?php comparison and if needed echo "alert('...')" else echo "#"?>

Double quotes for PHP and simple quotes for Javascript to avoid an error in interpretation.

Of course if you want to use PHP. You can also compare the dates in Javascript.

I am suggest you to add jquery.js,bootstrap.js and bootstrap date picker file and add following code. you don't need to validate for past date it will disable.

var date = new Date(); date.setDate(date.getDate()-1);

$('.txt-reg').datepicker({ startDate: date });

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