我使用以下代码(在 HAML 中)创建了一个 html 表单:
%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 = 'david.w.martin@me.com';
$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 = "david.w.martin@me.com";
$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 框架中)。
我迷路了,求救!!