php 注意:未定义索引。如何定义索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19128901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Notice: Undefined Index. How do I define the index?
提问by Mike
I received a response of:
我收到了以下回复:
Notice: Undefined index yy in Page1.php on line 11
Notice: Undefined index mm in Page1.php on line 12
Notice: Undefined index dd in Page1.php on line 13
注意:Page1.php 第 11 行未定义索引 yy
注意:第 12 行 Page1.php 中未定义索引 mm
注意:第 13 行 Page1.php 中未定义索引 dd
What do I do to define these yy, mm, dd?
我该怎么定义这些yy, mm, dd?
<?php
$redirect_page = 'Page2.php';
$_SESSION['yy'] = $_POST['yy']; // This is on line 11
$_SESSION['mm'] = $_POST['mm']; // This is on line 12
$_SESSION['dd'] = $_POST['dada']; // This is on line 13
if (isset($_POST['Submit'])) {
header('Location: ' . $redirect_page);
}
?>
<HTML>
<Form name="Page1" action="" method="POST">
<select name="yy">
<option value="2001">2001</option>
// ...Another 100 years
</select>
<select name="mm">
<option value="1">1</option>
<option value="2">2</option>
// ... another 10 months
</select>
<select name="dd">
<option value="1">1</option>
<option value="2">2</option>
// ...another 29 days
</select>
<input type="Submit" name="Submit" value="Submit">
</Form>
</HTML>
回答by Janzell Jurilla
Try wrapping those post variables in the issetfunction:
尝试将这些 post 变量包装在isset函数中:
$_SESSION['yy'] = isset($_POST['yy']) ? $_POST['yy'] : ""; // This is on line 11
$_SESSION['mm'] = isset($_POST['mm']) ? $_POST['mm'] : ""; // This is on line 12
$_SESSION['dd'] = isset($_POST['dada']) ? $_POST['dada'] : ""; // This is on line 13
回答by David Bélanger
I know peole already aswered but I will give you a little tip. Use this little function below :
我知道人们已经回答了,但我会给你一些提示。使用下面的这个小功能:
function _post($Var, $Default=''){
return (isset($_POST[$Var]) === TRUE ? $_POST[$Var] : $Default);
}
You can set the default value in case the variable is not found. It will also take care of the undefined index error ;)
如果找不到变量,您可以设置默认值。它还将处理未定义的索引错误;)
Undefined index mean that the entry was not found in the array. In this case, entries yy
, mm
and dd
doesn't exist in the $_POST
array. You have to check if they exists using the PHP language construct command isset. Use as follow :
未定义索引意味着在数组中找不到该条目。在这种情况下,数组中不存在条目yy
,mm
和。您必须使用 PHP 语言构造命令isset检查它们是否存在。使用如下:dd
$_POST
$_SESSION['yy'] = _post('yy', date('Y')); // will be the value of $_POST['yy'] or it not found will be the current year
$_SESSION['mm'] = _post('mm', date('M')); // ... or will be the current month (MM)
$_SESSION['dd'] = _post('dd', date('d')); // ... or will be the current day (DD)
You can create different function based on your needs :
您可以根据需要创建不同的功能:
function _get($Var, $Default=''){
return (isset($_GET[$Var]) === TRUE ? $_GET[$Var] : $Default);
}
function _session($Var, $Default=''){
return (isset($_SESSION[$Var]) === TRUE ? $_SESSION[$Var] : $Default);
}
function _put($Var, $Default=''){
return (isset($_PUT[$Var]) === TRUE ? $_PUT[$Var] : $Default);
}
function _delete($Var, $Default=''){
return (isset($_DELETE[$Var]) === TRUE ? $_DELETE[$Var] : $Default);
}
function _files($Var, $Default=NULL){
return (isset($_FILES[$Var]) === TRUE ? $_FILES[$Var] : $Default);
}
回答by Walter Gandarella
You have to start the PHP session before to set the values??:
您必须在设置值之前启动 PHP 会话??:
<?php
session_start();
$redirect_page= 'Page2.php';
$_SESSION['yy'] = $_POST['yy'];
$_SESSION['mm'] = $_POST['mm'];
$_SESSION['dd'] = $_POST['dada'];
// rest of code
?>
回答by Pramod
try
尝试
if(isset($_POST['yy']))
$_SESSION['yy'] = $_POST['yy'];
if(isset($_POST['mm']))
$_SESSION['mm'] = $_POST['mm'];
if(isset($_POST['dd']))
$_SESSION['dd'] = $_POST['dd']; // assuming it is $_POST['dd'] and not $_POST['dada']