1

I have made this simple php to check if a number is a positive or a negative. It works but I get this notice: Undefined index: submit.

<?php if (!$_POST["submit"]) { ?> <body> <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> Enter a number: <input name="number" size="2"> <input type="submit" name="submit" value="Go"> </form> </body> <?php } else { $number = $_POST['number']; if ($number > 0) { echo 'You entered a positive number'; } elseif ($number < 0) { echo 'You entered a negative number'; } else { echo 'You entered 0'; } } ?> 

Im new at php so if you can and want :)

1
  • When the page first loads there will be no values in $_POST values. $_POST will only be populated when your form is submitted. Commented Feb 6, 2014 at 9:47

5 Answers 5

1

You should replace the second line with:

if (!isset($_POST['submit'])) { 
Sign up to request clarification or add additional context in comments.

Comments

1

Your $_POST["submit"] appears to be empty. It will be inverted thanks to the ! and output true, so your program works.

But you never Post any value with the name submit, its the name of the button. I think you want to check if $_POST["number"] is empty.

if (!empty($_POST["number"])) { //your stuff here. 

Comments

0

Change your condition

if (!isset($_POST["submit"])) 

Comments

0

Replace

if (!$_POST["submit"]) 

with

if (isset($_GET['submit'])) 

Comments

0

Replace the second line:

if (!$_POST['submit']) 

with:

if (!isset($_POST['submit'])) 

1 Comment

This is more or less just a repeat of this existing answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.