jonesmeister Posted December 9, 2010 Share Posted December 9, 2010 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 More sharing options...
Maq Posted December 9, 2010 Share Posted December 9, 2010 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. Link to comment https://forums.phpfreaks.com/topic/221157-form-input-null-and-zero/#findComment-1145120 Share on other sites More sharing options...
Rifts Posted December 9, 2010 Share Posted December 9, 2010 <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."; } } ?> Link to comment https://forums.phpfreaks.com/topic/221157-form-input-null-and-zero/#findComment-1145122 Share on other sites More sharing options...
Pikachu2000 Posted December 10, 2010 Share Posted December 10, 2010 A heads-up on is_numeric, be aware that 81E456 is a valid number. If you want to validate that the input contains only the numbers 0-9, use ctype_digit. Link to comment https://forums.phpfreaks.com/topic/221157-form-input-null-and-zero/#findComment-1145218 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.