我需要一個非常簡單的,但有效的PHP形式處理,將這種形式的合作:PHP聯繫表單處理程序不工作?
<form id="contact" method="post" action="contact_handle.php">
<label for="name">Name:</label>
<input type="text" class="text_style" placeholder="John Doe" name="name" /><br />
<label for="email">Email:</label>
<input type="email" class="text_style" placeholder="[email protected]" name="email" /><br />
<label for="message">Subject:</label>
<textarea class="areawidth" rows="4" name="message" /></textarea><br />
<button id="contactbutton" type="submit">Submit</button>
</form>
這是我現在有,但它不發送電子郵件,它不重定向像我預期。我似乎無法弄清楚發生了什麼事。
<?php
$invalid = '';
$my_email = '[email protected]';
// Validate input:
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$invalid.= "\n All fields are required";
}
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Validate email:
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email))
{
$invalid .= "\n Invalid email address";
}
// Send email if no errors detected:
if(empty($invalid))
{
$to = $my_email;
$subject = "Contact form submission: $name";
$body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email\n Message:\n $message";
$headers = "From: $email\n";
$headers .= "Reply-To: $email";
mail($to,$subject,$body,$headers);
//redirect to the thank you page:
header('Location: contact_thanks.php');
}
?>
當它被提交時,它會帶我到contact_handle.php顯示空白,並無法重定向。
您是在本地主機上還是在線服務器上測試此功能?你是否收到任何錯誤訊息? – 2012-04-17 21:53:25
您的表單處理程序的第10行似乎存在拼寫錯誤 - 您將錯誤消息存儲在'$ error'變量中,而它應該是'$ invalid',這是其餘代碼使用的。 – Indrek 2012-04-17 21:56:24
糟糕,修正了錯字,儘管如此,結果仍然相同。我在本地主機和活動服務器上都進行了測試,兩者都不起作用。沒有錯誤信息顯示。只是一個空白的屏幕和電子郵件。 – 2012-04-17 22:00:23