我曾嘗試使用下面的代碼(HAML)創建的HTML表單:問題與電子郵件的形式提交POST方法
%form#cForm{method: 'post', name:'contactForm', action: 'formEmail.php'}
%input{type:'text', name: 'name'} Name/Business:
%input{type: 'text', name:'email'} Your Email:
%textarea{name: 'message'} Message:
%input{type:'submit', value:'Send Form'}
這裏是PHP文件(formEmail.php):
<?php
header('Content-type: text/html; charset=utf-8');
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = '[email protected]';
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "[email protected]";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
當我點擊「提交」我收到以下錯誤控制檯:
POST http://localhost:9000/formEmail.php [HTTP/1.1 404 Not Found 0ms]
,並出現在眉毛下面的消息呃:
Cannot POST /formEmail.php
但是,我可以直接訪問文件(通過輸入url到瀏覽器)。
表單位於模式彈出窗口(包含在Zurb Foundation框架中)內。
我迷路了,請幫忙!!
嘗試給它formEmail.php的完整路徑。它看起來可以找到它。如果您使用的是xampp或其他本地服務器,則發送郵件可能不起作用。 – Rocks 2013-04-05 23:53:55
沒有解決問題...我試圖「http:// localhost:9000/formEmail.php」在行動屬性 – davidwmartin 2013-04-06 06:20:50