2011-11-24 51 views
0

我收到$ _POST變量未定義的索引錯誤。 I.E. $ _ POST [ '主機']。我知道有點爲什麼我得到他們,但有沒有辦法讓PHP和窗體保持在同一個文件中,而不是得到這些錯誤?使用後未定義索引

<html> 
<head> 
    <title>Install Forum</title> 
</head> 
<body> 
<h1>Please enter your database information</h1> 

<form action='install.php' method='post'> 
Hostname: <input type='text' name='hostname'><br/> 
MySQL User: <input type='text' name='dbuser'><br/> 
MySQL Pass: <input type='password' name='dbpassword'><br/> 
<input type='submit' name='submit' value='Submit'><br/> 
</body> 
</html> 
<?php 
include_once("config.php"); 
$date = date('Y-m-d'); 

//Database Variables 
$dbhost = strip_tags($_POST['hostname']); 
$dbuser = strip_tags($_POST['dbuser']); 
$dbpass = strip_tags($_POST['dbpassword']); 
$submit = isset($_POST['submit']); 
+0

[PHP:「通知:未定義變量」和「通知:未定義指數」]的可能重複(http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Jocelyn

回答

4

裹在if你的PHP代碼與isset條件。這樣,代碼只會如果表單已提交運行:

if(isset($_POST['submit'])) 
{ 
    include_once("config.php"); 
    $date = date('Y-m-d'); 

    //Database Variables 
    $dbhost = strip_tags($_POST['hostname']); 
    $dbuser = strip_tags($_POST['dbuser']); 
    $dbpass = strip_tags($_POST['dbpassword']); 
    $submit = isset($_POST['submit']); 
} 
+0

ahh我明白了,謝謝。我試圖在每個變量上放置一個isset。像'isset(strip_tags($ _ POST ['hostname']));' – Darren

+0

嘗試'$ dbhost = isset($ _ POST ['hostname'])? strip_tags($ _ POST ['hostname']):FALSE;'或'if(isset($ _ POST ['hostname'])){$ dbhost = strip_tags($ _ POST ['hostname']); }' – Nathan