简体   繁体   English

我如何验证我的预订日期?

[英]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:使用如下所示的 strtotime() 函数:

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

instead of代替

if ($reserve <= $currentDate)

Your Javascript myFunction() is between 2 blocks of PHP and not inside PHP.您的 Javascript myFunction()位于 2 个 PHP 块之间,而不是在 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 "#"?> onsubmit="return myFunction()"可以变成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. PHP 的双引号和 Javascript 的简单引号以避免解释错误。

Of course if you want to use PHP.当然如果你想使用PHP。 You can also compare the dates in Javascript.您还可以在 Javascript 中比较日期。

I am suggest you to add jquery.js,bootstrap.js and bootstrap date picker file and add following code.我建议您添加 jquery.js、bootstrap.js 和引导日期选择器文件并添加以下代码。 you don't need to validate for past date it will disable.您不需要验证它会禁用的过去日期。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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