-2

The following code give undefined index in XAMPP but not on other APACHE Server is this error only limited to xampp

<?php
    if($_POST['names']!=""&&$_POST['n']!="")
    {
    print_r($_POST);
    }
?>

<form action="undefiend_error.php" method="post" >
<input type="text" name="names" value="">
<input type="text" name="n" value="">
<input type="hidden" name="nd" value="">
<input type="submit" value="submit" >
</form>

4 Answers4

1

This error will indeed be present on all systems, but your XAMPP server likely has display_errors = on in php.ini. If you view the error log of a server where you don't see the warning on screen, it should be logged unless error_reporting = 0 . If you don't see the error on other systems, that doesn't mean it isn't occurring - PHP is just configured not to show or log it.

You need to use isset() to test if the $_POST value is there, or you'll get an undefined index notice:

// Check that both `$_POST['names']` and `$_POST['n']` exist with `isset()`
if(isset($_POST['names']) && isset($_POST['n']) && $_POST['names'] != "" && $_POST['n']!="")
{
  print_r($_POST);
}
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • the cause is apparently not display_errors but error_reporting value. in fact, there are no word 'display' in the opening post. – Your Common Sense Jan 14 '12 at 18:53
  • no this code is runnig properly on my one machine but error on machine with XAMPP – VasudhaivaKutumbakam Jan 14 '12 at 18:53
  • @Col.Shrapnel Already added another bit about `error_reporting` – Michael Berkowski Jan 14 '12 at 18:55
  • @VasudhaivaKutumbakam The point is, this code will issue an undefined index warning on ANY PHP system where error_reporting is turned on. The code needs to be fixed as I have it above, and it will stop producing errors on your XAMPP system (which reports errors) and any other system you run it on which does not report errors. – Michael Berkowski Jan 14 '12 at 18:57
1

First of all, check if the form is submitted:

<?php
  if( $_SERVER['REQUEST_METHOD'] === 'POST' )
  {
     // Form submitted
  }
?>

And then you should use isset() to check if a post item is set. And at least you can use empty() to check if a post item is empty (or NULL or 0)

Wouter J
  • 41,455
  • 15
  • 107
  • 112
0

Undefined index/variable error is limited to erroneous code only.

An erroneous code would always produce an error, despite of the system you running. You can use some settings to gag the error message, but the error itself will remain in your code.

So, you have to corrrect the error, not error message.

To correct this one you have to check if there is should be a variable or not. The Wouter's method above os okay. So,

if( $_SERVER['REQUEST_METHOD'] === 'POST' ) 
{ 
  print_r($_POST); 
} 

will raise an error only when there is absent required field - the thing error messages are for.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
-2

PHP reminds you that every variable you use must be defined first. Or, if it coming from outside, checked for existence. try this

if(!empty($_GET))?$vars=$_GET:$vars=error;
echo $vars['n'];
echo $vars['names'];
Bipin Chandra Tripathi
  • 2,550
  • 4
  • 28
  • 45