Jump to content

Form Input NULL and ZERO


jonesmeister

Recommended Posts

Hello again. I need help for a PHP project I'm working on for school.

 

I need it to do the following:

1) When nothing is entered, it will echo "Please enter a number."

2) When "0" is entered, it will echo "Please enter a number that is not zero."

 

My code is as follows...

<form action="hwpositive.php" method="post">
    Enter number <input type="text" name="number"/><br />
    </form>
    
<?php
$input = $_POST['number'];

	// if number entered is less than 0
	if ($input<0) 
		{ 
			echo "Enter a positive number.";
		}
	// if number entered is greated than 1000
	elseif ($input>1000)
		{ 
			echo "Enter a number less than 1000.";
		}
	// if number entered is not a number (ie, a letter, a character)
	elseif (!is_numeric($input))
		{
			echo "<br><img src=\"squidward.jpg\"><br><br>Enter a valid number. ";
		}
	// if number entered is between 1-999
	elseif (($input>0) && ($input<1000))
		{ 
			echo "hello world " . $i . "<br />";
		}
	// if number entered is zero
	elseif ($input===0)
		{
			echo "Please enter a number that is not zero.";
		}
	// if no number is entered
	elseif ($input=null)
		{
			echo "Please enter a number.";
		}

	else
		{
			echo "Please enter a number.";
		}	
?>

 

However, it's not working. What am I doing wrong? Everytime I enter this page, it should show me "Please enter a number" because I haven't entered anything yet, but it doesn't. It's showing me the image right away. I don't understand... Help me, please!

 

Thank you so much in advance. I appreciate the help.

Link to comment
https://forums.phpfreaks.com/topic/221157-form-input-null-and-zero/
Share on other sites

elseif (!is_numeric($input))

 

Since you haven't inputted anything in your form then $input == NULL.  Which this elseif is true because NULL isn't numeric so your logic is correct.  You should check to see if $_POST['number'] isset and if it is then assign $input to it, if not, initialize $input to something else.

<form action="" method="post">
    Enter number <input type="text" name="number"/><br />
<input type="submit">
    </form>
    
<?php
if(isset($_POST['number'])) {
$input = $_POST['number'];


	// if number entered is less than 0
	if ($input<0) 
		{ 
			echo "Enter a positive number.";
		}
	// if number entered is greated than 1000
	elseif ($input>1000)
		{ 
			echo "Enter a number less than 1000.";
		}
	// if number entered is not a number (ie, a letter, a character)
	elseif (!is_numeric($input))
		{
			echo "<br><img src=\"squidward.jpg\"><br><br>Enter a valid number. ";
		}
	// if number entered is between 1-999
	elseif (($input>0) && ($input<1000))
		{ 
			echo "hello world " . $input . "<br />";
		}
	// if number entered is zero
	elseif ($input==0)
		{
			echo "Please enter a number that is not zero.";
		}
	// if no number is entered
	elseif ($input=null)
		{
			echo "Please enter a number.";
		}

	else
		{
			echo "Please enter a number.";
		}	
		}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.