Reputation: 1
I have been getting the following errors in my php file:
Notice: Undefined index: errStr in /clientdata/zeus-dynamic-1/c/r/crowndoor.com.au/www/crowntest/contact.php on line 13
Notice: Undefined index: sent in /clientdata/zeus-dynamic-1/c/r/crowndoor.com.au/www/crowntest/contact.php on line 20
which appear at the top of the browser window. I have tried turning off notices using: error_reporting(E_ALL ^ E_NOTICE); but it doesn't seem to make any difference. I am now attempting to fix the undefined indexes. my php is as follows:
<?php
session_name("fancyform");
session_start();
$_SESSION['n1'] = rand(1,20);
$_SESSION['n2'] = rand(1,20);
$_SESSION['expect'] = $_SESSION['n1']+$_SESSION['n2'];
$str='';
if($_SESSION['errStr'])
{
$str='<div class="error">'.$_SESSION['errStr'].'</div>';
unset($_SESSION['errStr']);
}
if (!isset($_POST['errStr']))
{
//If not isset -> set with dumy value
$_POST['errStr'] = "undefine";
}
$success='';
if($_SESSION['sent'])
{
$success='<h1>Thank you!</h1>';
$css='<style type="text/css">#contact-form{display:none;}</style>';
unset($_SESSION['sent']);
}
?>
If anyone has any thoughts about how to fix these notices from appearing that would be great.
Upvotes: 0
Views: 451
Reputation: 76
Add code on the top of the php page.
<?php error_reporting(0); ?>
Upvotes: 1
Reputation: 8520
Turn of notices like:
error_reporting(E_ALL & ~E_NOTICE
)
and/or check the existance of the array keys first.
$sent = array_key_exists('sent', $_SESSION) ? $_SESSION['sent'] : null;
Upvotes: 0
Reputation: 111
You can however check manually yourself without interfering the php.ini setting as it will add more complication on the deployment when you migrate your server one day.
To check manually, please see the following code...you have done it in a line somewhere in your code though...
<?php
if (isset($_SESSION['sent'])) {
$success='<h1>Thank you!</h1>';
$css='<style type="text/css">#contact-form{display:none;}</style>';
unset($_SESSION['sent']);
}
?>
Upvotes: 0
Reputation: 522625
It means the array index you're looking for does not exist. The two ways to handle that are to either make sure it exists, then this error message would be pointing out a logic flaw in your app. Or, if the index may legitimately not exist, use isset
to check before accessing it. See The Definitive Guide To PHP's isset And empty.
Upvotes: 1
Reputation: 11700
Change line 13:
if($_SESSION['errStr'])
To:
if(isset($_SESSION['errStr']))
Upvotes: 0
Reputation: 160973
You need to check the key exists or not first.
if(isset($_SESSION['errStr']) && $_SESSION['errStr']) {
Upvotes: 0
Reputation: 8468
Why don't you use isset()
just like you do with $_POST['errStr']
?
Upvotes: 0