3

我收到了以下回复:

注意:第 11 行 Page1.php 中未定义的索引 yy

注意:第 12 行 Page1.php 中未定义的索引 mm

注意:第 13 行 Page1.php 中未定义的索引 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>
4

4 回答 4

6

尝试将这些 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
于 2013-10-02T02:08:01.460 回答
6

我知道人们已经回答了,但我会给你一点小费。在下面使用这个小功能:

function _post($Var, $Default=''){
    return (isset($_POST[$Var]) === TRUE ? $_POST[$Var] : $Default);
}

如果找不到变量,您可以设置默认值。它还将处理未定义的索引错误;)

未定义的索引意味着在数组中找不到该条目。yy在这种情况下,条目mmdd不存在于$_POST数组中。您必须使用 PHP 语言构造命令isset检查它们是否存在。使用如下:

$_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)

您可以根据需要创建不同的功能:

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);
}
于 2013-10-02T02:16:12.537 回答
2

您必须先启动 PHP 会话才能设置值​​​:

<?php 
   session_start(); 

   $redirect_page= 'Page2.php';

   $_SESSION['yy'] = $_POST['yy'];
   $_SESSION['mm'] = $_POST['mm'];
   $_SESSION['dd'] = $_POST['dada'];

   // rest of code
?>
于 2013-10-02T02:08:48.433 回答
1

尝试

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']
于 2013-10-02T02:15:55.423 回答