2011-09-09 57 views
0

我是新來的Stackoverflow,並認爲我把我的第一個問題給你精彩的人。一個PHP表格的HTML輸出

我有一個PHP表單,我需要把它作爲一個HTML模板。成功的消息做得很好。但是通過電子郵件發送的消息不會這樣做。這是我的代碼示例。除HTML輸出外,表單運行良好。我把標籤放在裏面看它是否有效,但標籤不轉換。

任何幫助將不勝感激。

<?php 
if(isset($_POST['email'])) { 

// EDIT THE 2 LINES BELOW AS REQUIRED 
$email_to = ($_POST['email']); 
$email_subject = "Dear ".($_POST['first_name']). ", someone you know sent you this  email."; 


function died($error) { 
    // your error code can go here 
    echo "<h4>We are very sorry, but you left a field blank or incorrect. </h4>"; 
    echo "Maybe you need to be slapped.<br /><br />"; 
    echo $error."<br /><br />"; 
    echo "Please press the back button to resend redo.<br /><br />"; 
    die(); 
} 

// validation expected data exists 
if(!isset($_POST['first_name']) // ||!isset($_POST['last_name']) 
    ||!isset($_POST['email']) 
    //||!isset($_POST['telephone']) 
    //||!isset($_POST['comments']) 
    ) { 
    died('We are very sorry, but left a field blank or incorrect.');  
} 

$first_name = $_POST['first_name']; // required 
// $last_name = $_POST['last_name']; // required 
$email_from = "[email protected]"; // required 
// $telephone = $_POST['telephone']; // not required 
// $comments = $_POST['comments']; // required 

$error_message = ""; 
$email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
if(!preg_match($email_exp,$email_from)) { 
$error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
} 
$string_exp = "/^[A-Za-z .'-]+$/"; 
if(!preg_match($string_exp,$first_name)) { 
$error_message .= 'The First Name you entered does not appear to be valid.<br />'; 
} 
// if(!preg_match($string_exp,$last_name)) { 
// $error_message .= 'The Last Name you entered does not appear to be valid.<br />'; 
// } 
// if(strlen($comments) < 2) { 
// $error_message .= 'The Comments you entered do not appear to be valid.<br />'; 
// } 
if(strlen($error_message) > 0) { 
died($error_message); 
} 
    // Set the Headers for HTML E-mail 
    $headers = 'MIME-Version: 1.0' . "\r\n"; 
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 


// email message sent to the person getting slapped 
$email_message = "Someone has just sent you an email.\n\n"; 



function clean_string($string) { 


    $headers = "Content-Type: text/html; charset=iso-8859-1\n"; 
    $email_message = " This is a test <strong> of the HTML </strong>"; 

} 
$email_message = " This is a test <strong> of the HTML </strong>"; 







// $email_message .= "First Name: ".clean_string($first_name)."\n"; 
// $email_message .= "Last Name: ".clean_string($last_name)."\n"; 
// $email_message .= "Email: ".clean_string($email_from)."\n"; 
// $email_message .= "Telephone: ".clean_string($telephone)."\n"; 
// $email_message .= "Comments: ".clean_string($comments)."\n"; 


// create email headers 
$headers = 'From: '.$email_from."\r\n". 
'Reply-To: '.$email_from."\r\n" . 
'X-Mailer: PHP/' . phpversion(); 
@mail($email_to, $email_subject, $email_message, $headers); 
?> 

<!-- include your own success html here --> 

Thank you for sending your <strong>email</strong>. <br /> 

Please press the back button 



<?php 
} 
?> 

回答

1

你取消了你的頭:

$headers = "Content-Type: text/html; charset=iso-8859-1\n"; 

,那麼你必須:

$headers = 'From: '.$email_from."\r\n". 
'Reply-To: '.$email_from."\r\n" . 
'X-Mailer: PHP/' . phpversion(); 
@mail($email_to, $email_subject, $email_message, $headers); 

,而不是嘗試:

$headers = "Content-Type: text/html; charset=iso-8859-1\n"; 

$headers .= 'From: '.$email_from."\r\n". 
    'Reply-To: '.$email_from."\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 
    @mail($email_to, $email_subject, $email_message, $headers); 

的=在第二次分配。 $ headers變量連接在一起,在你身上r代碼,您將用第二個任務替換$標頭。

+0

感謝凱文您的答案。我試過了,輸出結果仍然是「這是HTML的測試」。它沒有大膽的話。有什麼建議麼? –