Undefined index in PHP

Possible duplicate:
PHP: "Note: Undefined variable" and "Notice: Undefined index"

Good day!

There is 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 to remove the error above? Thanks.

+8
php
source share
4 answers

This should be a notification, not an error.

To fix this, you need to check if $_POST['submit'] :

 if(!isset($_POST['submit'])) { ... } 
+14
source share

Here, where you check that it is not. It should be !isset($_POST['SUBMIT']) . This is due to the fact that the SUBMIT index will not be set, therefore it will not have a value, for example true, to pass if(...) . isset() checks if the index / variable is actually set.

Try the following:

 <?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 } ?> 
+7
source share
 <?php if (!isset($_POST['SUBMIT'])){ //ERROR: Undefined index ?> 

This is testing if the index is set.

+4
source share

Options:

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

Of these, the third option is certainly the best. You should not assume that any variable provided by the user will be set as you expect; You should always check that it is installed at all, and also that it is set to the expected values. Otherwise, you may be open to cracking attacks.

+3
source share

All Articles