$ _POST未定义索引:错误

时间:2013-10-02 12:36:22

标签: php post server-side

表格:

<form action="/HW2/controllers/login.php" id = "login-box" method = "post">
        Username: <input type="text" name='user'><br>
        Password: <input type="text" name='pwd'><br>
        <input type="submit">
    </form>

行动:

<?php
require("../config/config.php");
if(isset($_POST['user']) && isset($_POST['pwd'])){
    if(strcmp($_POST['user'], USERNAME) == 0 && strcmp($_POST['pwd'], 
        PASSWORD) == 0){
        session_start();
        $_SESSION['logon'] = true;
        echo "success";
        //header("location: /HW2/index.php?view=loggedin");
    }else{
        print_r("Sorry, your username and/or password are invalid. Try Again?");
        //header("location: /HW2/index.php?view=loginPage");
        }
}else{
    print_r("Post data not sent.");
}
?>

我尝试了很多不同的方法,所有这些都导致“发布数据未发送。”(又名“未定义的索引:”,如果我尝试访问$ _POST值)试图找到我搞砸了好几个现在几个小时,任何帮助都表示赞赏,我是一个完全磨砂的PHP,它可能是非常简单和愚蠢的东西。

提前致谢!

3 个答案:

答案 0 :(得分:0)

我认为不需要使用最后的&#34;否则&#34;

仅使用

if(isset($_POST['user']) && isset($_POST['pwd'])){
if(strcmp($_POST['user'], USERNAME) == 0 && strcmp($_POST['pwd'], 
    PASSWORD) == 0){
    session_start();
    $_SESSION['logon'] = true;
    echo "success";
    //header("location: /HW2/index.php?view=loggedin");
}else{
    print_r("Sorry, your username and/or password are invalid. Try Again?");
    //header("location: /HW2/index.php?view=loginPage");
    }

}

答案 1 :(得分:0)

<?php
error_reporting(E_ALL ^ E_NOTICE);

require("../config/config.php");
if(isset($_POST['user']) && isset($_POST['pwd'])){
    if(strcmp($_POST['user'], USERNAME) == 0 && strcmp($_POST['pwd'], 
        PASSWORD) == 0){
        session_start();
        $_SESSION['logon'] = true;
        echo "success";
        //header("location: /HW2/index.php?view=loggedin");
    }else{
        print_r("Sorry, your username and/or password are invalid. Try Again?");
        //header("location: /HW2/index.php?view=loginPage");
        }
}else{
    print_r("Post data not sent.");
}
?>

error_reporting(E_ALL ^ E_NOTICE); 显示错误但隐藏了通知。未定义的索引显示$ _POST变量在没有值的情况下被调用,因此这是第一个选项。第二个选项是修改代码。

观点:

<form action="/HW2/controllers/login.php" id = "login-box" method = "post">
        Username: <input type="text" name='user'><br>
        Password: <input type="text" name='pwd'><br>
        <input type="submit" name = "submit" value = "Submit">
    </form>

Php代码:

<?php
require("../config/config.php");
if(isset($_POST['submit'])){
    if(strcmp($_POST['user'], USERNAME) == 0 && strcmp($_POST['pwd'], 
        PASSWORD) == 0){
        session_start();
        $_SESSION['logon'] = true;
        echo "success";
        //header("location: /HW2/index.php?view=loggedin");
    }else{
        print_r("Sorry, your username and/or password are invalid. Try Again?");
        //header("location: /HW2/index.php?view=loginPage");
        }
}else{
    print_r("Post data not sent.");
}?>

答案 2 :(得分:0)

嘿谢谢你的答案,但是我重新启动计算机并小睡后,我的代码工作正常。

我的猜测是,无论是chrome还是apache都错误地处理了$ _POST变量,但我并不是很有经验,所以我只是吐痰。无论哪种方式,我的代码都在做我想要的。

感谢您的帮助!