XAMPP未指定的错误

时间:2012-04-17 17:57:38

标签: php html

我知道XAMPP在Win 7上遇到了很多问题。所以我成功安装了它,现在我有了非特定的错误,这是我以前从未有过的。

我在HTML中有一个简单的标签

<form method="post" action="site.php">
  <input type="text" name="NAME">
</form>

我的PHP代码也很简单:

<?php 
  $something = $_POST['NAME'];
?>

当我启动XAMPP并在“htdocs”中打开此HTML时出现问题/通知:

  

注意:未定义的索引:第40行的C:\ xampp \ htdocs \ test.php中的wert

这只是一个XAMPP错误吗?因为我之前从未遇到过这个问题,而且看起来很安静。我想我正在使用XAMPP 1.7.7。

问候:)

2 个答案:

答案 0 :(得分:2)

当您使用之前尚未设置的数组索引的值时,会弹出“未定义索引”通知:

<?php
$myarray = array('a'=>1, 'b'=>2);
var_dump($myarray['a']); //outputs int(1) as this is defined
var_dump($myarray['c']); //we defined 'a' and 'b' but not 'c'.
?>

第三行将给出:“注意:未定义的索引:c在第3行的C:\ xampp \ htdocs \ test.php”。

当您访问$ _GET或$ _POST数组时,通常会发生这种情况。大多数情况下,原因是你拼错了索引(例如你键入了$_POST['tset']而不是$_POST['test']),或者因为你在HTML表单中编辑了一个提交信息的<input>元素然后忘记了重新调整PHP代码。

通过使用isset()测试索引是否存在,您可以确保一切正常:

if( isset($_POST['test']) ) {
    $myvar = $_POST['test'];
    //and then whatever else you intended
}
else {
    //the index wasn't defined - you made a mistake, or possibly the user deliberately removed an input from the submitting form
    $error = "POST-array is missing the index 'test'!";
    //and now make sure that things after this which rely on the 'test' value don't run
}

您可以在 lot 脚本中找到一个非常常见的行:

$myvar = isset($_POST['test']) ? $_POST['test'] : 'N/A';

这使用了if-else结构的特殊PHP简写。这一行几乎完全相同:

if( isset($_POST['test']) ) {
    $myvar = $_POST['test'];
}
else {
    $myvar = 'N/A';
}

答案 1 :(得分:0)

你应该这样做:

<?php
if(isset($_POST['NAME'])){
    $something = $_POST['NAME'];
}
?>

当您打开该页面时,在您提交该数据之前不会有$_POST['NAME']

相关问题