9

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

Good day!

I am having the following error in my code:

<?php
if (!$_POST['SUBMIT']){   //ERROR: Undefined index
?>
    <H2>Add Employee</H2>
    <form action="<?php print $_SERVER['PHP_SELF']; ?>" method="POST">
    <table width="400" border="0" cellspacing="1" cellpadding="2">
       <tr>
            <td width="100">SSN</td>
            <td><input name="SSN" type="text" id="SSN"></td>
       </tr>
       <tr>
            <td width="100">&nbsp;</td>
            <td><input name="SUBMIT" type="SUBMIT" id="ADD" value="ADD"></td>
       </tr>
    </table>
    </form>
 <?php
    }
    else {
    //code here
    }
?>

How can I remove the error above? Thank you.

Community
  • 1
  • 1
newbie
  • 14,582
  • 31
  • 104
  • 146

4 Answers4

15

It should be a notice and not an error.

To fix is you'll have to check whether $_POST['submit'] is set:

if(!isset($_POST['submit'])) {
    ...
}
gmhk
  • 15,598
  • 27
  • 89
  • 112
halfdan
  • 33,545
  • 8
  • 78
  • 87
7

It's where you test to see that is isn't there. It should be !isset($_POST['SUBMIT']). This is because the index, SUBMIT, won't be set, thus won't have a value such as true to pass the if(...). isset() checks to see if the index/variable is actually set.

Try this:

<?php
    if (!isset($_POST['SUBMIT'])){   //ERROR: Undefined index
?>
<H2>Add Employee</H2>
<form action="<?php print $_SERVER['PHP_SELF']; ?>" method="POST">
<table width="400" border="0" cellspacing="1" cellpadding="2">
   <tr>
        <td width="100">SSN</td>
        <td><input name="SSN" type="text" id="SSN"></td>
   </tr>
   <tr>
        <td width="100">&nbsp;</td>
        <td><input name="SUBMIT" type="SUBMIT" id="ADD" value="ADD"></td>
   </tr>
</table>
</form>
<?php
    }
    else {
    //code here
    }
?>
Tanner Ottinger
  • 2,970
  • 4
  • 22
  • 28
4
<?php
  if (!isset($_POST['SUBMIT'])){   //ERROR: Undefined index
?>

This tests, if the index is set

Florian
  • 3,145
  • 1
  • 27
  • 38
3

Options:

  • Disable warning messages, by editing the setting in PHP.ini
  • Add an @ sign in front of the variable name, which will suppress the error on that particular line.
  • Change your code to use isset($_POST['SUBMIT']) before checking it further.

Of these, the third option is definitely the best. You shouldn't assume that any variable supplied by the user will be set as you expect; you should always check that it's set at all, and also that it's set to the expected values. Otherwise you are liable to be open to hacking attacks.

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • 1
    The first two bullet points you suggest are bad practice and I wouldn’t recommend. The only time you want to disable warnings are when you run on production. Never use @, ever. There’s always a better alternative. – Ollie Saunders Nov 26 '11 at 23:34
  • @Ollie - Granted I stated that the options are available, but I didn't recommend them; I explicitly stated that the `isset()` option was the one to go for. – Spudley Nov 27 '11 at 18:10