0
我目前正在與驗證一個簡單的PHP代碼的工作,這是我的代碼自我後的PHP代碼:與驗證
<form action="" method="post">
<input type="text" name="name" id="name" placeholder="Name">
<br>Number of Books:<select id="books" name="books">
<option> 1 </option>
<option> 2 </option>
<option> 3 </option>
<option> 4 </option>
<option> 5 </option>
<option> 6 </option>
<option> 7 </option>
<option> 8 </option>
<option> 9 </option>
<option> 10 </option>
</select>
</br>dutedate <input type="date" name="duedate" id="duedate" placeholder="duedate">
</br>borrow <input type="date" name="datebor" id="datebor" placeholder="dateborrowed">
</br>return <input type="date" name="ret" id="ret" placeholder="date returned">
<input type="submit" class="button" name="btnsubmit" value="Submit">
</form>
<?php
if (isset($_POST['btnsubmit']))
{
$name = $_POST['name'];
$duedate = date_create(($_POST['duedate']));
$datebor = date_create(($_POST['datebor']));
$ret = date_create(($_POST['ret']));
$books = $_POST['books'];
$NOWdate = new Datetime('now');
if ($duedate < $datebor)
{
echo "<script>alert('Wrong Input Error1')</script>";
exit;
}
elseif ($datebor > $ret)
{
echo "<script>alert('Wrong Input Error2')</script>";
exit;
}
elseif ($datebor > $NOWdate)
{
echo "<script>alert('Wrong Error3')</script>";
exit;
}
elseif ($ret < $duedate)
{
$penalty = 0 ;
}
else
{
$penaltydays = date_diff($duedate,$ret) ;
$diff_date = $penaltydays->format('%a');
$penalty = $diff_date * $books * 10;
}
}
?>
<p>Penalty: <?php echo $penalty ?></p>
當我嘗試運行它,有一個錯誤Undefined variable: penalty
。
我敢肯定,我的驗證碼是否正確
我怎麼能忽略處罰變量頁面本身。
我認爲這會解決錯誤。
try $ penalty ='';在$ NOWdate = new Datetime('now')之後; –