Jump to content

Using the isset function in php I want to display a confirmation message but its not showing properly


Go to solution Solved by Barand,

Recommended Posts

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. However I keep getting '1 is not leap year' regardless of the the year I enter. What am I doing wrong?

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<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 (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>";
		}
}
?>
<h1>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>

 

Ok i have entered this code:

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

To which it now returns the values I entered but now it showing all entered values as not being leap years. I used 2020 and 2021 as the test values.

  • Solution

try

<?php
if (isset($_GET["leapyear"]) && !empty($_GET['leapyear'])) {
    
    $result =  (is_leapyear($_GET["leapyear"])) ?
                   "<span style=\"color:#008631;\">{$_GET['leapyear']} is a leap year</span>"  :
                   "<span style=\"color:#FF0000;\">{$_GET['leapyear']} is not a leap year</span>" ;
}
else $result = '' ;

function is_leapyear($year){

    if(is_numeric($year)){
        if($year%4 == 0) {
            if($year%100 == 0) {
                return ($year%400 == 0);
            }
            else return true; 
        }
    }
    return false;
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Leap year form</title>
</head>
<body>

<?= $result ?>

<h1>Leap Year</h1> 
<form  method = "get" >
<label for="leapyear">Enter a year</label>
<input type="text" name="leapyear" id="leapyear" autofocus/>
<p><input type="submit" name="confirm" value="Check For Leap Year" /></p>
 </form>
</body>
</html>

 

function is_leapyear($year){

    if(is_numeric($year)){
        if($year%4 == 0) {                       // if it's divisible by 4 its probably a leapyar
            if($year%100 == 0) {                 // but check the century condition
                return ($year%400 == 0);         // if its divisible by 400 return true otherwise return false
            }
            else return true; 
        }
    }
    return false;                                // if we get to here, return false
}

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.