我预订表格,我需要一个日期验证。
如果日期小于当前日期,则应显示一条提示信息"请选择更高的日期"留在页面上更改日期。但是我的警报存在问题:当我选择更高的日期时,它仍会显示警报消息。
这是我的代码:
<!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>
答案 0 :(得分:1)
使用strtotime()函数,如下所示:
if (strtotime($reserve) <= strtotime($currentDate))
而不是
if ($reserve <= $currentDate)
答案 1 :(得分:0)
您的Javascript myFunction()
介于2个PHP块之间,而不是PHP内部。然后加载无条件。
您必须以不同方式构建代码。 onsubmit="return myFunction()"
可能会成为onsubmit=<?php comparison and if needed echo "alert('...')" else echo "#"?>
PHP的双引号和Javascript的简单引号,以避免解释错误。
当然,如果你想使用PHP。您还可以在Javascript中比较日期。
答案 2 :(得分:0)
我建议你添加jquery.js,bootstrap.js和bootstrap日期选择器文件并添加以下代码。你不需要验证它将禁用的过去日期。
var date = new Date(); date.setDate(date.getDate() - 1);
$( 'TXT-REG')。日期选择器({ startDate:date});