Home > Blockchain >  Using the isset function in php I want to display a confirmation message but its not showing properl
Using the isset function in php I want to display a confirmation message but its not showing properl

Time:09-26

Basically I created a leapyear calculator, but to improve user experience i want it to display a message on the webpage confirming whether or not the year entered is a leapyear without changing the webpage.

<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($year0 ==0)
                if($year@0 ==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>

The result I get is "1 is a leapyear" and not the entered input. Where did I make the mistake?

CodePudding user response:

I think you have made a mistake in the year checking condition. Please see below updated code:

$year = isset($_GET["leapyear"]) ? $_GET["leapyear"] : 0 ;

Added above line to set year value in $year if set otherwise zero

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