dongmubi4444 2016-10-07 17:57
浏览 21

PHP $ _POST数组空

i'm new in coding and have a problem coding on Mac.

When i do var_dump($_POST), answer is: array(0){}

Here is the code:

<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name=strip_tags($_POST["name"]);
    $age=$_POST["age"]*1;
    $_SESSION["name"] = $name;
    $_SESSION["age"] = $age;
}
else {
    $name = $_SESSION["name"];
    $age = $_SESSION["age"];
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
    <title>Session demonstration</title>
</head>

<body>
<h1>Session demonstration</h1>
<a href="session-2.php">Demo session</a><br>
<a href="session_destroy.php">Close session</a><br><br>
<form action="<?=$_SERVER["PHP_SELF"]?>"
      method="post">
    Your name is:
    <input type="text" name="name" value="<?php echo $name?>"><br>
    You are:
    <input type="text" name="age" value="<?php echo $age?>"><br>
    <input type="submit" value="Submit">
</form>
<?php
if ($name and $age) {   
    if ($name and $age) {
        echo "<h1>Hello, $name</h1>";
        echo "<h3>You are $age</h3>";
    }
    else {
        print "<h3>Bye!</h3>";
    }
}
?>
</body>
</html>

Same time, when i press submit button it shows me:

Notice: Undefined index: name in /Applications/MAMP/htdocs/PHP_Course 2/demo/mod2/sessions/session-1.php on line 5

Notice: Undefined index: age in /Applications/MAMP/htdocs/PHP_Course 2/demo/mod2/sessions/session-1.php on line 6

Maybe someone knows what can i do.

Sincerelly

  • 写回答

1条回答 默认 最新

  • duandang2838 2016-10-07 18:21
    关注

    the problem with this is your session variables are not set at the beginning and you are trying to assign your $name and $age with those unassigned variables on your else block. Validate your assignments like this.

    <?php
    session_start();
    $name = $age = "";
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $name = strip_tags($_POST["name"]);
        $age = $_POST["age"] * 1;
        $_SESSION["name"] = $name;
        $_SESSION["age"] = $age;
    } elseif (isset($_SESSION['name']) || isset($_SESSION['age'])) {
        if (isset($_SESSION['name'])) {
            $name = $_SESSION['name'];
        }
        if (isset($_SESSION['age'])) {
            $age = $_SESSION['age'];
        }
    }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
    <html>
        <head>
            <title>Session demonstration</title>
        </head>
    
        <body>
            <h1>Session demonstration</h1>
            <a href="session-2.php">Demo session</a><br>
            <a href="session_destroy.php">Close session</a><br><br>
            <form action="<?= $_SERVER["PHP_SELF"] ?>" method="post">
                Your name is:
                <input type="text" name="name" value="<?php echo $name ?>"><br>
                You are:
                <input type="text" name="age" value="<?php echo $age ?>"><br>
                <input type="submit" value="Submit">
            </form>
    <?php
    
        if ($name and $age) {
            echo "<h1>Hello, $name</h1>";
            echo "<h3>You are $age</h3>";
        } else {
            print "<h3>Bye!</h3>";
        }
    ?>
        </body>
    </html>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器