簡體   English   中英

$ _POST未定義索引:錯誤

[英]$_POST Undefined index: Error

表格:

<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完全了解,這可能是非常簡單和愚蠢的事情。

提前致謝!

我認為無需使用最后一個“ else”

僅使用

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");
    }

}

<?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
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.");
}?>

嘿,謝謝大家的回答,原來,在我重新啟動計算機並小睡之后,我的代碼工作正常。

我的猜測是chrome或apache錯誤地處理了$ _POST變量,但是我不是很有經驗,所以我只是在吐口水。 無論哪種方式,我的代碼都可以完成我現在想要的工作。

仍然感謝您的幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM