我想使用PHP做一個聯繫表格,並且有一些問題。我是PHP的新手,所以無法弄清楚。如果沒有應用驗證碼,但表單適用,但由於我應用了驗證代碼,因此可能需要某些字段,表單不能正常工作。此外,當我留下任何必填字段爲空時,他們形成不顯示任何錯誤消息。有人可以告訴問題是什麼。我的PHP聯繫表格的一些問題
HTML表單
<form action="mail.php" method="POST" >
Name: <input type="text" name="name"><br/><br/>
Email: <input type="email" name="email"><br/><br/>
Phone Number: <input type="text" name="phone_number"><br/><br/>
Website: <input type="text" name="website"><br/><br/>
Message: <textarea name="message" rows="6" cols="25"></textarea><br/><br/>
<input type="submit" value="Submit">
</form>
主要PHP腳本文件
<?php
if(isset ($_POST['submit'])) {
$errors = array();
if(!empty ($_POST ['name'])) {
$name = $_POST ['name'];
} else {
$errors[] = "You forgot to enter your Name.";
}
if(!empty ($_POST ['email'])) {
$email = $_POST ['email'];
} else {
$errors[] = "You forgot to enter your Email.";
}
if(!empty ($_POST ['message'])) {
$message = $_POST ['message'];
} else {
$errors[] = "You forgot to enter your Message.";
}
$phone_number = $_POST['phone_number'];
$website = $_POST['website'];
$formcontent = "From: $name \n Email: $email \n Phone Number: $phone_number \n Website: $website \n Message: $message";
$recipient = "[email protected]";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($formcontent, $recipient, $subject, $mailheader);
if(isset($_POST['submit'])) {
if(!empty($errors)) {
foreach ($errors as $msg)
{
echo '<li>'. $msg . '</li>';
}
} else {
echo "Thank You";
}
}
}
?>
UPDATE 謝謝您回覆傢伙,我幾乎忘了具有提交按鈕的名稱屬性。這有助於展示一些結果。但是現在一些通知顯示未定義變量爲$formcontent
和$mailheader
行的電子郵件,消息(如果我僅提供表單中的名稱並點擊提交按鈕)。
既然你」重新使用'!empty',你最好把這個代碼取出來,並使表單域成爲'required'添加該屬性,尤其是在使用HTML5的情況下。 – 2015-02-05 16:04:57
'if(isset($ _POST ['submit']))'您沒有提交的名稱屬性來支持它,因此它內部沒有任何用處。在使用[錯誤報告](http://php.net/manual/en/function.error-reporting.php)後,會拋出未定義索引提交警告/通知。 – 2015-02-05 16:05:40
您的提交按鈕沒有'name ='submit'',所以'$ _POST ['submit']'可能是空的。郵件的東西都應該在其他地方,你有'回聲'謝謝你''因爲你不想郵件,如果有錯誤。你不需要檢查'ifset($ _ POST ['submit'])'兩次是多餘的 – 2015-02-05 16:06:03