【问题标题】:Using the isset function in php I want to display a confirmation message but its not showing properly在php中使用isset函数我想显示一条确认消息,但它没有正确显示
【发布时间】:2022-09-26 15:23:59
【问题描述】:

基本上我创建了一个闰年计算器,但为了改善用户体验,我希望它在网页上显示一条消息,确认输入的年份是否是闰年而不更改网页。

<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 是闰年”,而不是输入的输入。我在哪里犯了错误?

  • 你好,记得缩进你的代码,这样更易​​理解,对你以后有很大的帮助!

标签: php html forms


【解决方案1】:

我认为您在年份检查条件中犯了一个错误。请参阅下面的更新代码:

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>";
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-20
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    相关资源
    最近更新 更多