2012-08-12 170 views
1

我很抱歉什麼是一個非常基本的問題,但我很新的網頁設計,我只是想讓我的表單與PHP一起工作。非常簡單的PHP問題我需要幫助

表單填寫正常,我收到一封電子郵件,但沒有收到任何正文文本。我沒有告訴我這條消息是什麼,我得到這個:

您已收到用戶fgafdfa的新消息。 這裏是信息: [email protected]

正如你所看到的名稱有(剛剛輸入廢話),但是缺少信息,而不是我得到出現在我自己的電子郵件地址...

下面是窗體的HTML:

<div id="form"> 
        <form action="php/send_form_email.php" method="post" > 
         <span>Name</span> 
         <input type="text" name="name" class="name" /> 
         <span>Email</span> 
         <input type="text" name="email" class="email"/> 
         <span>Message</span> 
         <textarea class="message"></textarea> 
         <input type="submit" name='submit' value="submit" class="submit"> 
        </form> 
       </div> 

這裏的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']; 
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"; 
mail($to,$email_subject,$email_body,$headers); 
header('Location: ../index.htm'); 
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; 
    } 
} 

?> 

誰能幫助?

+2

你已經用'class =「message」'設置了textarea的類,但也許你還想在該textarea上設置'name =「消息」'。 – ghbarratt 2012-08-12 19:31:55

回答

1

兩個錯誤:

查看textarea的HTML中缺少的名稱。更正後的代碼:

<textarea name="message" class="message"></textarea> 

的代碼,你不終止分配到$ EMAIL_BODY用分號而是由點追加到它。只是有點運氣,你沒有得到解析錯誤。更正後的代碼:

$email_body = "You have received a new message from the user $name.\n". 
"Here is the message:\n $message"; 

$to = "[email protected]"; 
+0

哇。這就是它所需要的。不夠感謝你! – Tim 2012-08-12 19:44:22