Reputation: 118
I'm using this code to check a number if it's an even or an odd number. I'm learning PHP and when I run this code it gives an unusual error.
<html>
<body>
<head>
<title>Judging even and odd numbers</title>
</head>
<form method = "post" action="EAO.php" >
<font size = "20">Please enter a number to check if it is even or odd:</font>
<input type = "text" name = "number" />
<input type = "hidden" name="checker" value="true" />
<input type = "submit" value = "submit" />
</form>
<?PHP
if (isset($_POST['checker'])) {
$number = $_POST['number'];
if ($number % 2 == 0 ) {
echo "The number is Even";
}
if($number % 2 == 1 ) {
echo "The number is odd";
}
if ($number == "") {
echo "Please enter a number";
}
}
?>
</body>
</html>
It works good when you input a number but when you submit the form empty, Its says "Please enter a number. The number is even". Help me correct it if I'm doing something wrong. If my code isn't of standard to judge a number, please write your code that is better. Thank you.
Upvotes: 3
Views: 5113
Reputation: 217
another working version is (improved security):
<?php
if (isset($_POST['checker']) && $_POST['checker'] == 'true') {
if (empty($_POST['number'])) { // 0 or empty values will be blocked
echo "Please enter a number";
} else {
if (ctype_digit($_POST['number'])) { // only number will be accepted
$number = $_POST['number'];
if ($number % 2 == 0 ) {
echo "The number is Even";
}
if($number % 2 == 1 ) {
echo "The number is odd";
}
else {
echo 'Numbers only please!';
}
}
}
?>
Upvotes: 1
Reputation: 2525
Try this :
<html>
<head>
<title>Judging even and odd numbers</title>
</head>
<body>
<form method = "post" action="" >
<font size = "20">Please enter a number to check if it is even or odd:</font>
<input type = "text" name = "number" />
<input type = "hidden" name="checker" value="true" />
<input type = "submit" value = "submit" />
</form>
<?PHP
if (isset($_POST['checker'])) {
$number = $_POST['number'];
if(empty($number))
{
echo "Please enter a number";
}
elseif ($number % 2 == 0 ) {
echo "The number is Even";
}
else {
echo "The number is odd";
}
}
?>
</body>
</html>
Place body tag after head tag. If you are posting the form to the same page then action parameter is optional. Use if...elseif...else condition http://www.w3schools.com/php/php_if_else.asp
Upvotes: 1
Reputation: 165
HI bro here we go with you code
<html>
<body>
<head><title>Judging even and odd numbers</title></head>
<form method = "post" action="" >
<font size = "20">Please enter a number to check if it is even or odd:</font>
<input type = "text" name = "number" />
<input type = "hidden" name="checker" value="true" />
<input type = "submit" value = "submit" name="submit" />
</form>
<?PHP
if (isset($_POST['submit'])) {
$number = $_POST['number'];
if ($number % 2 == 0 ) {
echo "The number is Even";
}
if($number % 2 == 1 ) {
echo "The number is odd";
}
if ($number == "") {
echo "Please enter a number";
}
}
?>
</body>
</html>
Upvotes: 0
Reputation: 640
Just reversing your logic will do the magic for you. You must always use if () {....} elseif () {....}
so that only one logic runs at a given time.
<html>
<head>
<title>Judging even and odd numbers</title>
</head>
<body>
<form method = "post" action="EAO.php" >
<font size = "20">Please enter a number to check if it is even or odd:</font>
<input type = "text" name = "number" />
<input type = "hidden" name="checker" value="true" />
<input type = "submit" value = "submit" />
</form>
<?PHP
if (isset($_POST['checker'])) {
$number = $_POST['number'];
if ($number == "") {
echo "Please enter a number";
}
else if ($number % 2 == 0 ) {
echo "The number is Even";
}
else if($number % 2 == 1 ) {
echo "The number is odd";
}
}
?>
</body>
</html>
Upvotes: 3