未定义的索引:发布

时间:2014-03-13 14:20:46

标签: php forms indexing

使用联系表单我发现xampp会返回以下错误:

注意:未定义的索引:在第18行的/Applications/XAMPP/xamppfiles/htdocs/demo/demo2.php中发送

注意:未定义的变量:第39行的/Applications/XAMPP/xamppfiles/htdocs/demo/demo2.php中的css

在此代码中:

<?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']);
}
?>

但另一方面,它也在标签内部出现错误(如此):

注意:未定义的索引:发布在 /Applications/XAMPP/xamppfiles/htdocs/demo/demo2.php 的第 68

第68行是以下一行,似乎错误在value=""左右,但只要我不是一个PHP大师,谁知道如何解决以下错误?

第68行示例:

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

2 个答案:

答案 0 :(得分:1)

是的,我们第一次打开页面然后没有设置索引你必须使用

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

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

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

答案 1 :(得分:1)

你得到Undefined index:post因为在开头你的$ _SESSION实际上不包含它...你可以通过在回复之前检查它是否存在来摆脱它:

<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'] : ''); ?>" />
相关问题