Mohammad Saberi
Mohammad Saberi

Reputation: 13166

Incorrect "Notice: Undefined index" in PHP

I have a project that uses the codes bellow:

Page 1:

<form action="customersPrint.php" method="post" target="_blank">
   Customer's name: <input type="text" name="search1" /><br />
   <input type="submit" value="Search" />
</form>

Page 2 (customersPrint.php):

<?php
echo $_POST['search1'];
?>  

The strange problem is that the code in page 2 works fine on local machine and a production server. But it returns the message below in another remote server:

Notice: Undefined index: search1 in ...

This behavior is wrong. Because I've posted this parameter and also, it did not returned any similar message before.
What's the problem ?

More information:
Using the code below, I informed that page 2 says nothing has been sent to it:

if (isset($_POST['search1'])) echo 'Sent'; else echo 'Not sent';

Result was Not sent.

Also var_dump($_POST) returns nothing as result.

Upvotes: 0

Views: 481

Answers (3)

james
james

Reputation: 26271

There may be a solution involving Mod Security. Check out this link: http://www.s2member.com/kb/mod-security-random-503-403-errors/

Try adding this to the root .htaccess file(taken from the above link):

# Mod Security v1.x.
# May work in .htaccess too, on some hosts.
<IfModule mod_security.c>
    SecFilterEngine Off
    SecFilterScanPOST Off
</IfModule>

# Mod Security v2.x.
# Will NOT work in .htaccess, use httpd.conf.
<IfModule mod_security2.c>
    SecRuleEngine Off
</IfModule>

Or if you have access to edit the httpd.conf file(taken from the above link):

# Mod Security v2.x only.
# Will NOT work in .htaccess, use httpd.conf.
<IfModule mod_security2.c>
    SecRuleRemoveById 960024 981173 981212 960032 960034
</IfModule>

Upvotes: 0

SArnab
SArnab

Reputation: 585

If you are attempting to access customersPrint.php directly without submitting the form, then the $_POST variable is empty. The message you are receiving is not a fatal error, but just a notice that you are trying to output something that does not exist.

To fix this, check to see if the variable is set before the echo. For example:

echo (isset($_POST['search1'])) ? $_POST['search1'] : ''; 

Upvotes: 0

putvande
putvande

Reputation: 15213

Your localhost probably has all the error_reporting off where as your production server doesn't. So it is normal behaviour that you get this error message because $_POST['search1'] does not exist when you first visit the page.

You can put a check around it to overcome this situation:

<?php
    if (isset($_POST['search1'])) {
        echo $_POST['search1'];
    } 
?> 

Also, I would turn off all error_reporting on your production website as it isn't very professional to show the errors to your visitors.

http://www.php.net/manual/en/function.error-reporting.php

Upvotes: 0

Related Questions