2

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

I am learning PHP and I have used the post method to submit form data and I can not know why I get those two errors:

Notice: Undefined index: search_for in \xampp\htdocs\samir\indexes.php on line 5

Notice: Undefined index: replace_with in \xampp\htdocs\samir\indexes.php on line 6

if(isset($_POST['user_input']) && isset($_POST['search_for']) && isset($_POST['replace_with']) 
    && !empty($_POST['user_input']) && !empty($_POST['search_for']) && !empty($_POST['replace_with']))
    echo $user_input = $_POST['user_input'];
    echo $search_for = $_POST['search_for'];
    echo $replace_with = $_POST['replace_with'];
Community
  • 1
  • 1

3 Answers3

3

You forgot to add enclosing brackets for your if statement, like so:

if(isset($_POST['user_input']) && isset($_POST['search_for']) 
    && isset($_POST['replace_with']) && !empty($_POST['user_input']) 
    && !empty($_POST['search_for']) && !empty($_POST['replace_with'])) {
    echo $user_input = $_POST['user_input'];
    echo $search_for = $_POST['search_for'];
    echo $replace_with = $_POST['replace_with'];
}

the above code should do what you want.

Nelson
  • 49,283
  • 8
  • 68
  • 81
  • 1
    Just to clarify, without enclosing braces, only the first command after the `if` statement is treated as being conditional - so your 2nd and 3rd echo will be executed whether the result of your `if` is true or false – Basic Sep 20 '12 at 09:45
1

When using $ _POST or $ _GET to retrieve the variables from a form, you may encounter this error:

Notice: Undefined index 'fields of the table' in 'path of php file being executes' on line 'current line'

This error appears because of your PHP error reporting settings. Usually, it appears when your variable is not properly set. There are two ways to handle this issue:

1.Check if $_POST['action'] or $GET['action'] is set before using it. For example:

if (!isset($_POST['action'])) {//your pure stuff   }       

 if (!isset($_GET['action'])) {//your pure stuff   } 

2. Suppress Notice warnings

Notice warnings could be suppressed by changing the error_reporting variable in your PHP.ini. error_reporting could be set to show all errors except those for notices and coding standards warnings: error_reporting = E_ALL & ~E_NOTICE

<?php error_reporting (E_ALL ^ E_NOTICE); ?>

but my personal suggestion is solve the warning instead of use the 2 method


updated question answer

you haven't add enclosing brackets {} so the only one line after if will consider as in if body and your 2nd and 3rd echo will be executed whether the result of your if is true or false

it should be

if(isset($_POST['user_input']) && isset($_POST['search_for']) 
    && isset($_POST['replace_with']) && !empty($_POST['user_input']) 
    && !empty($_POST['search_for']) && !empty($_POST['replace_with'])) {
    echo $user_input = $_POST['user_input'];
    echo $search_for = $_POST['search_for'];
    echo $replace_with = $_POST['replace_with'];
}
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
1

If you are using the variable before the form as been posted, e.g $var = $_POST['var']; It will return an error.

It is best to check if the submit button has been pressed.

Example:

if(isset($_POST['submit'])){
    //form has been posted
}

Then, I would make sure that all the post variables that you will be using are set, if not throw an error.

Example:

$error = false;
//['submit'] is the button used to post the form.
if(isset($_POST['submit'])){
    $testVar = (isset($_POST['testVar']) && strlen($_POST['testVar']) > 0 ? $_POST : $error[] = 'Please enter something.');

    if(!$error){
        //Submit for
    }
    else{
        foreach($error as $e){
            echo $e;
        }
    }
}
CharliePrynn
  • 3,034
  • 5
  • 40
  • 68