Xavi Alsina
Xavi Alsina

Reputation: 653

Undefined index: post

Working on a contact form I found the xampp returns me the following error:

Notice: Undefined index: sent in /Applications/XAMPP/xamppfiles/htdocs/demo/demo2.php on line 18

Notice: Undefined variable: css in /Applications/XAMPP/xamppfiles/htdocs/demo/demo2.php on line 39

Inside this code:

<?php
session_name("fancyform");
session_start();


$_SESSION['n1'] = rand(1,20);
$_SESSION['n2'] = rand(1,20);
$_SESSION['expect'] = $_SESSION['n1']+$_SESSION['n2'];


$str='';
if(isset($_SESSION['errStr'])){
    $str='<div class="error">'.$_SESSION['errStr'].'</div>';
    unset($_SESSION['errStr']);
}

$success='';
if($_SESSION['sent'])
{
    $success='<h1>Thank you!</h1>';

    $css='<style type="text/css">#contact-form{display:none;}</style>';

    unset($_SESSION['sent']);
}
?>

But in the other hand, its also poping errors inside the label (like this one):

Notice: Undefined index: post in /Applications/XAMPP/xamppfiles/htdocs/demo/demo2.php on line 68

The line 68 is the following one, and seems like the error is around the value="" but as long as I am not a kind of PHP guru, anyone knows how to solve the following error?

Line 68 example:

<input type="text" class="validate[required,custom[onlyLetter]] form-control" name="name" required id="name" value="<?=$_SESSION['post']['name']?>" />

Upvotes: 0

Views: 133

Answers (2)

Pavel Petrov
Pavel Petrov

Reputation: 730

You get Undefined index: post because in the beginning your $_SESSION actually does not contain it...You can get rid of the Notice by checking if it exists before echo-ing it:

<input type="text" class="validate[required,custom[onlyLetter]] form-control" name="name" required id="name" value="<?php echo (isset($_SESSION['post']['name']) ? $_SESSION['post']['name'] : ''); ?>" />

Upvotes: 1

Ravi Sharma
Ravi Sharma

Reputation: 578

Yes first time when we open the page then the index is not set you have to use

if(isset($_SESSION['sent']))
{
$success='<h1>Thank you!</h1>';

$css='<style type="text/css">#contact-form{display:none;}</style>';

unset($_SESSION['sent']);
}

Upvotes: 1

Related Questions