2017-02-03 56 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" />