Jump to content

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

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.