2017-05-30 55 views
0

當我點擊提交按鈕時,即使我設置了PHP,也沒有任何反應。起初我以爲我的代碼顯然是錯誤的。但現在我甚至從這裏複製/粘貼代碼,這被檢查爲正確的,但沒有任何反應。當我點擊我的聯繫表單中的提交按鈕時,沒有任何反應

這裏是代碼:

<?php 
if(isset($_POST['submit'])){ 
    $to = "[email protected]"; // this is your Email address 
    $from = $_POST['email']; // this is the sender's Email address 
    $first_name = $_POST['first_name']; 
    $last_name = $_POST['last_name']; 
    $subject = "Form submission"; 
    $subject2 = "Copy of your form submission"; 
    $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message']; 
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message']; 

    $headers = "From:" . $from; 
    $headers2 = "From:" . $to; 
    mail($to,$subject,$message,$headers); 
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender 
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly."; 
    // You can also use header('Location: thank_you.php'); to redirect to another page. 
} 
?> 

<!DOCTYPE html> 
<head> 
<title>Form submission</title> 
</head> 
<body> 

<form action="" method="post"> 
First Name: <input type="text" name="first_name"><br> 
Last Name: <input type="text" name="last_name"><br> 
Email: <input type="text" name="email"><br> 
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br> 
<input type="submit" name="submit" value="Submit"> 
</form> 

</body> 
</html> 


</div> 

這會不會是服務器中的問題,而不是實際的代碼?因爲我嘗試了其他幾個我在各種教程中找到的例子,並且發生了同樣的問題。

還是我還在做錯什麼?

+1

回顯是否打印「郵件發送」? –

+1

請你可以在你的代碼塊開頭刪除''。 – manian

+0

它是否顯示任何類型的錯誤,如「警告:郵件():無法連接到郵件服務器」。或者當您提交時打印某些內容? – manian

回答

0

至少,我會做如下修改代碼:

<?php 
$msg=''; 
if (isset($_POST['submit'])) 
{ 

...

if (!mail($to,$subject,$message,$headers)) 
     error_log(($msg='Send form submission to "'.$to.'" failed')); 
    else if (!mail($from,$subject2,$message2,$headers2)) // sends a copy of the message to the sender 
     error_log(($msg='Send copy of form submission to user "'.$from.'" failed')); 
    else $msg='Mail Sent. Thank you ' . $first_name . ', we will contact you shortly.'; 

...

<body> 
<?php if ($msg) 
{ ?> 
<p><?php echo $msg ?></p> 
<?php 
} ?> 
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> 

這些改變將不會解決公關形式的oblem易受發送垃圾郵件,但它會解決這些問題:如果發生mail()呼叫錯誤

  • ,它會被記錄下來,並會顯示一條消息(這也將確認所涉及的郵件地址)
  • 單引號字符串不必由PHP解釋器進行解析,除非你是嵌入需要
  • 你會不會產生被解釋變量或字符序列(如"\n")應使用在文檔的<head>部分
  • 之前將文本寫入瀏覽器210
  • 您可以放心正確使用action來處理表格
相關問題