2017-02-03 57 views
0

当我点击提交按钮时,它会带我到.php文件,就好像它是一个链接?它没有运行脚本。我查看了其他问题,似乎无法找到任何与脚本相关的实际运行情况,因此不胜感激。PHP Form to Email not working;加载PHP页面而不是运行脚本

HTML

<article id="contact"> 
    <h2 class="major">Contact</h2> 
    <form name="contact-form" method="post" action="contact.php"> 
     <div class="field half first"> 
      <label for="name">Name</label> 
      <input type="text" name="name" id="name" /> 
     </div> 
     <div class="field half"> 
      <label for="email">Email</label> 
      <input type="text" name="email" id="email" /> 
     </div> 
     <div class="field"> 
      <label for="message">Message</label> 
      <textarea name="message" id="message" rows="4"></textarea> 
     </div> 
     <ul class="actions"> 
      <li><input type="submit" value="Send Message" class="special" name="submit" /></li> 
      <li><input type="reset" value="Reset" /></li> 
     </ul> 
    </form> 

PHP

<?php 
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]';//<== update the email address 
$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]";//<== update the email address 
$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; 
    } 
} 

?> 
+0

你是如何访问此html页面?你是否通过双击.html文件直接运行它? – newbie

回答

0

很难告诉你你的意思形式不运行..什么是您的服务器配置为解析PHP文件?

如果是这样,你没有叫输入“提交”,以便

if(!isset($_POST['submit']))  

是不正确的。多个解决方案,但我认为最简单的是添加一个隐藏的输入名称提交您的窗体内的某处...

<input type="hidden" name="submit" value="1" />