[英]how can i validate the date for my reservation?
我做了一個預訂表格,我需要一個日期驗證。
如果日期小於當前日期,它應該顯示一條警告消息,說“請選擇一個更高的日期”停留在頁面上以更改日期。 但是我的警報有問題:當我選擇更高的日期時,它仍然顯示警報消息。
這是我的代碼:
<!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>
使用如下所示的 strtotime() 函數:
if (strtotime($reserve) <= strtotime($currentDate))
代替
if ($reserve <= $currentDate)
您的 Javascript myFunction()
位於 2 個 PHP 塊之間,而不是在 PHP 內部。 然后它無條件加載。
您必須以不同的方式構建代碼。 onsubmit="return myFunction()"
可以變成onsubmit=<?php comparison and if needed echo "alert('...')" else echo "#"?>
PHP 的雙引號和 Javascript 的簡單引號以避免解釋錯誤。
當然如果你想使用PHP。 您還可以在 Javascript 中比較日期。
我建議您添加 jquery.js、bootstrap.js 和引導日期選擇器文件並添加以下代碼。 您不需要驗證它會禁用的過去日期。
var date = new Date(); date.setDate(date.getDate()-1);
$('.txt-reg').datepicker({ startDate: date });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.