基本上我創建了一個閏年計算器,但為了改善用戶體驗,我希望它在網頁上顯示一條訊息,確認輸入的年份是否是閏年而不更改網頁。
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="keywords" content="Web,programming" />
<title>Leap year form</title>
</head>
<body>
<?php
$year = isset($_GET["leapyear"]);
function is_leapyear($year){
if(is_numeric($year)){
if($year%4 ==0)
if($year%100 ==0)
if($year%400 ==0){
return true;
}
return false;
}
}
if (isset($_GET["confirm"])){
if ($year == true){
echo'<span style="color:#008631;">';
echo"$year is a leap year</span>";
}else{
echo'<span style="color:#FF0000;">';
echo "$year is not a leap year</span>";
}
}
?>
<h1>Lab 03 Task 2 - Leap Year</h1>
<form action = "leapyear_selfcall.php" method = "get" >
<label for="leapyear">Enter a year</label>
<input type="text" name="leapyear"/>
<p>
<input type="submit" name="confirm" value="Check For Leap Year"/>
</p>
</form>
</body>
</html>
我得到的結果是“1 是閏年”,而不是輸入的輸入。我在哪里犯了錯誤?
uj5u.com熱心網友回復:
我認為您在年份檢查條件中犯了一個錯誤。請參閱下面的更新代碼:
$year = isset($_GET["leapyear"]) ? $_GET["leapyear"] : 0 ;
如果設定為零,則添加以上行以在 $year 中設定年份值
if (isset($_GET["confirm"])){
if (is_leapyear($year)){
echo'<span style="color:#008631;">';
echo"$year is a leap year</span>";
}
else
{
echo'<span style="color:#FF0000;">';
echo "$year is not a leap year</span>";
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/510122.html
標籤:phphtml形式
