2016-12-31 88 views
3

我試圖創建一個使用php的聯繫表單,並有一些麻煩。

條件:isset($_POST['submit'])總是會產生錯誤,即使我只是提交一個空白頁。

這裏是我的代碼:

contact.html部分:

<form action='emailto.php' method='POST' enctype='text/plain'> 
First Name:<br> 
<input type='text' name='firstname'><br> 
Last Name:<br> 
<input type='text' name='lastname'><br> 
Email Address:<br> 
<input type='text' name='emailadd' ><br> 
Subject:<br> 
<input type='text' name='subject' ><br> 
Message:<br> 
<textarea name='message' rows=5 cols=30 ></textarea><br><br> 
<input type='submit' value='Send' name='submit'> 
</form> 

emailto.php:

<?php 
if (isset($_POST['submit'])) { 
$to = "emailaddress"; 
$subject = $_POST['subject']; 
$firstname = $_POST['firstname']; 
$lastname = $_POST['lastname']; 
$Emailadd = $_POST['emailadd']; 
$Message = $_POST['message']; 
$Body= ""; 
$Body .= $firstname; 
$Body .= $lastname; 
$Body .= "\n"; 
$Body .= $Emailadd; 
$Body .= "\n"; 
$Body .= $Message; 
mail($to,$subject,$Body); 
echo "Mail Sent! <a href='contact.html'>Go Back</a>"; 
} else { 
header("Location: contact.html"); 
exit(0); 
} 
?> 

此外,有什麼奇怪的是,如果我刪除if-else語句在emailto.php, 提交後,將會出現一個錯誤信息:未定義的索引:主題,名字,姓氏,電子郵件,消息...

我完全困惑.. 期待聽到一些建議。 在此先感謝。

+0

另請注意,isset()對於定義爲空字符串的變量(例如空白POST數據字段)返回true。 –

回答

2

從您的表格中刪除這enctype='text/plain'它會開始工作; 我保證。如果它失敗了,那麼你需要找出原因並確保郵件可供你使用。

旁註:您還應該檢查是否有任何輸入是空的(並且required有幫助)。

,並使用正確的(全)標頭mail()

否則,您可以mailout被視爲垃圾郵件或乾脆拒絕。

作爲郵件第四個參數的一部分,應該有一個有效的From: <email>

I.e.從手冊:

<?php 
$to  = '[email protected]'; 
$subject = 'the subject'; 
$message = 'hello'; 
$headers = 'From: [email protected]' . "\r\n" . 
    'Reply-To: [email protected]' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

mail($to, $subject, $message, $headers); 
?> 

「再說了,有什麼奇怪的是,如果我刪除emailto.php的if-else語句,提交後,會出現一個錯誤信息:undifined指數:主題,姓,姓氏,emailadd,消息...」

  • 再次;這是由enctype='text/plain'引起的,它不是用於使用POST數組的有效enctype。

編輯:

添加if/elsemail()。如果它回聲「休斯頓我們有一個問題」,那麼你的問題就出現了。

如果回顯「郵件已發送!」但沒有收到郵件,然後看看你的垃圾郵件。郵件已經完成了它的工作,你需要找出它從未被髮送/接收的原因。

<?php 
if (isset($_POST['submit'])) { 
$to = "emailaddress"; 
$subject = $_POST['subject']; 
$firstname = $_POST['firstname']; 
$lastname = $_POST['lastname']; 
$Emailadd = $_POST['emailadd']; 
$Message = $_POST['message']; 
$Body= ""; 
$Body .= $firstname; 
$Body .= $lastname; 
$Body .= "\n"; 
$Body .= $Emailadd; 
$Body .= "\n"; 
$Body .= $Message; 

if(mail($to,$subject,$Body)){ 
echo "Mail Sent! <a href='contact.html'>Go Back</a>"; 
} else { echo "Houston, we have a problem"; } 

} else { 
header("Location: contact.html"); 
exit(0); 
} 
?> 
+2

類似於:http://stackoverflow.com/questions/7628249/method-post-enctype-text-plain-are-not-compatible – Progrock

+0

@Progrock是的。但是,爲了幫助他們,我在答案中添加了一些內容。希望一切都會順利並按預期進行。 –

+0

THX!真的很有幫助! –

相關問題