2013-11-28 48 views
1

我的電子郵件php表單出現問題,當我點擊提交按鈕時,其他頁面出現提示There was a problem with your e-mail() 我不是沒有我做錯了什麼?PHP的郵件表格不起作用

這裏是我的代碼:

HTML代碼

<!-- Subscription Form --> 
    <form class="email" action="form.php" method="post"> 
    <input class="get_notified" type="text" placeholder="Enter your email address ..."/> 
    <button type="submit" class="go" /></form> 
<!-- End Subscription Form --> 

    </div> 
    </div> 
</body> 

PHP代碼

<?php 
$to = "[email protected]"; 
$from = "[email protected]"; 

$headers = "From: " . $from . "\r\n"; 

$subject = "New subscription"; 
$body = "New user subscription: " . $_POST['email']; 


if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) 
{ 
if (mail($to, $subject, $body, $headers, "-f " . $from)) 
{ 
    echo 'Your e-mail (' . $_POST['email'] . ') has been added to our mailing list!'; 
} 
else 
{ 
    echo 'There was a problem with your e-mail (' . $_POST['email'] . ')'; 
} 
} 
else 
{ 
echo 'There was a problem with your e-mail (' . $_POST['email'] . ')'; 
} 
+1

代碼中的兩個錯誤消息完全相同,所以您不知道哪個'else'塊觸發了它。將其中一條消息更改爲不同的消息,以查明哪個條件評估爲false。 –

+1

嘗試將代碼量減少到只有郵件功能,並查看它是否有效,如果是,則開始逐位添加您的代碼 –

+1

Dulax向您提供了代碼無效的原因,將name屬性添加到表單中領域 –

回答

3

你需要一個name="email"字段添加到您的HTML表單,以便PHP能夠使用$_POST['email']

<input class="get_notified" name="email" type="text" placeholder="Enter your email address ..."/>