我是一個初學者的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表單指向自己的地方。我怎樣才能解決這個問題?當涉及到內容和數據處理的分離時,這甚至是不錯的做法嗎?
我
請參閱http://stackoverflow.com/search?q=%5Bphp%5D+undefined+index+%24_POST。有許多許多相關的問題,並且解決方案總是相同的 - 'isset($ _ POST ['key'])' – 2012-02-26 13:03:34
請參閱我的答案的更新。 – 2012-02-26 13:18:27
歡迎來到SO--因爲這是你的第一個問題,它可以幫助我們很好地使用一個非常具有描述性的問題標題,而不是像PHP/HTML問題那樣模棱兩可。如果您的標題包含您正在研究的錯誤消息的一部分,那麼在您提交之前,SO將會搜索相關問題並在其中提交。 – 2012-02-26 13:19:50