用于表单电子邮件提交的POST方法问题

时间:2013-04-05 23:47:41

标签: php forms email

我使用以下代码(在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框架中)。

我迷路了,请帮助!!

1 个答案:

答案 0 :(得分:0)

尝试将http://localhost:9000/formEmail.php放入action属性中。也许端口被忽略了,它试图达到http://localhost/formEmail.php,这当然是404。