2012-02-26 71 views
0

可能重複:
Undefined Index when using postPHP/HTML的問題:(

我是一個初學者的PHP開發人員,我只是用一些東西玩弄我試圖創建一個簡單的登記表:

爲registration.php

<?php 

include('Auth.php'); 

$first_name = $_POST['first_name']; 
$last_name = $_POST['last_name']; 
$email= $_POST['email']; 
$password = $_POST['password']; 

$auth = new Auth(); 
$auth->register($first_name, $last_name, $email, $password); 

?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Registration</title> 
</head> 
<body> 
    <form action="registration.php" method="POST"> 

     <p> First Name: <input type="text" size="20" name="first_name" id="first_name" value="" /> </p> 
     <p> Last Name: <input type="text" size="40" name="last_name" id="last_name" value="" /></p> 
     <p> Email: <input type="text" size="60" name="email" id="email" value="" /></p> 
     <p> Password: <input type="password" size="16" name="password" id="password" value="" /></p> 

     <input type="submit" value="submit" id="submit"/> 
    </form> 
</body> 
</html> 

我得到這些錯誤:

Notice: Undefined index: first_name in C:\xampp\htdocs\Store\Store\Controller\Registration.php on line 6 

Notice: Undefined index: last_name in C:\xampp\htdocs\Store\Store\Controller\Registration.php on line 7 

Notice: Undefined index: email in C:\xampp\htdocs\Store\Controller\Registration.php on line 8 

Notice: Undefined index: password in C:\xampp\htdocs\Store\Store\Controller\Registration.php on line 9 

我猜這是因爲因爲沒有已提交的POST變量裏面什麼呢。我在互聯網上看到過,php處理表單和html表單在同一個文件中,而不是像我正在做的那樣的兩個單獨的文件。 html表單指向自己的地方。我怎樣才能解決這個問題?當涉及到內容和數據處理的分離時,這甚至是不錯的做法嗎?

+0

請參閱http://stackoverflow.com/search?q=%5Bphp%5D+undefined+index+%24_POST。有許多許多相關的問題,並且解決方案總是相同的 - 'isset($ _ POST ['key'])' – 2012-02-26 13:03:34

+0

請參閱我的答案的更新。 – 2012-02-26 13:18:27

+0

歡迎來到SO--因爲這是你的第一個問題,它可以幫助我們很好地使用一個非常具有描述性的問題標題,而不是像PHP/HTML問題那樣模棱兩可。如果您的標題包含您正在研究的錯誤消息的一部分,那麼在您提交之前,SO將會搜索相關問題並在其中提交。 – 2012-02-26 13:19:50

回答

1

請注意,這些只是通知,您應該在開發環境中啓用這些通知,但在您的網站上線時禁用。如果您沒有訪問php.in文件,你可以做到這一點通過使用此行代碼:

ini_set("display_errors", 0); 

您的問題,現在,這的確是事實,你的變量是空的。您可以設置一個名稱元素到您的提交按鈕假設 <input type="submit" value="submit" name="submit" id="submit"/>

然後:

if (isset($_POST['submit'])) 
{ 

include('Auth.php'); 

$first_name = $_POST['first_name']; 
$last_name = $_POST['last_name']; 
$email= $_POST['email']; 
$password = $_POST['password']; 

$auth = new Auth(); 
$auth->register($first_name, $last_name, $email, $password); 

} 

這樣的話,你會確保當你的具體形式提交的代碼,這部分將只執行。

-1

我猜這是因爲因爲沒有已提交的POST變量裏面什麼呢。

對!第一次加載表單時,沒有POST變量。
所以,你必須要麼移動註冊碼分開的文件或籤,如果它是POST請求,開始處理它:

if ($_SERVER['REQUEST_METHOD'] == 'POST') 
    include('Auth.php'); 

    $first_name = $_POST['first_name']; 
    $last_name = $_POST['last_name']; 
    $email= $_POST['email']; 
    $password = $_POST['password']; 

    $auth = new Auth(); 
    $auth->register($first_name, $last_name, $email, $password); 
} 

這甚至很好的做法,當涉及到的內容和數據處理分離?

那麼,它不是一個更好的。要將顯示邏輯與數據處理分開,您可能希望將表單移動到單獨的文件中幷包含它。但是,對於包含20行的小腳本,這不是必需的。

欲瞭解更多關於分離的信息,請參考我最近寫的答案:https://stackoverflow.com/a/9433418/285587隨意問問是否有什麼不清楚的地方。雖然我希望它很簡單,但完全可行的這種分離和表單處理的示例很容易學習