2013-04-05 135 views
0

我曾尝试使用下面的代码(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框架中)内。

我迷路了,请帮忙!!

+0

尝试给它formEmail.php的完整路径。它看起来可以找到它。如果您使用的是xampp或其他本地服务器,则发送邮件可能不起作用。 – Rocks 2013-04-05 23:53:55

+0

没有解决问题...我试图“http:// localhost:9000/formEmail.php”在行动属性 – davidwmartin 2013-04-06 06:20:50

回答

0

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

+0

,这没有奏效... – davidwmartin 2013-04-06 06:20:16

+0

你使用.htaccess文件来重写你的URL也许? – 2013-04-08 11:43:13

+0

你的www根文件夹中有formEmail.php文件吗?你能告诉我你用来查看你有表格的html页面的URL吗? – 2013-04-08 11:47:15