2011-08-31 18 views
1

我正在嘗試爲網站創建並提交表單並在所有字段提交後發送郵件。數據通過ajax調用提交。不同瀏覽器中的表單編碼問題

問題是,我的網站用戶使用UTF-8工作,如果我使用Internet Explorer提交表單,則信息(來自表單)不會以UTF-8發送,除非我執行utf8_encode()。但是,如果我發送郵件使用ut8_encode功能Firefox和Chrome停止工作

的一些注意事項的電子郵件...: - 含形式的PHP文件頭中UTF-8 - 元標記UTF-8 - 在(其中,將表單提交文件)的ajax.php也已在標題UTF-8 - 電子郵件標頭是以UTF-8字符集接收(獨立於瀏覽器的)

代碼: ajax.php

$email = $_REQUEST['email']; 
$phone = $_REQUEST['phone']; 
$reply = $_REQUEST['reply']; 

$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; 
$headers .= 'From: '.$email . "\r\n" . 
'X-Mailer: PHP/' . phpversion(); 

$topic = 'subject'; 
$message = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head> 
<body style="font-size:11px; font-family:Verdana, sans-serif; line-height:17px;"> 
    <h1 style="color:#255670; font-size:16px; font-family: \'Trebuchet MS\', sans-serif; text-transform:uppercase;">Quizz</h1> 
    <div style="background-color:#EFEFEF; padding:10px;"> 
     Nome: <b>'.$name.'</b><br /> 
     Telefone: <b>'.$phone.'</b><br /> 
     E-mail: <a href="mailto: '.$email.'"><b>'.$email.'</b></a><br /> 
    </div> 
    <div style="border-top:1px dashed #8CD3D7; padding:5px; margin:10px 0px 0px 0px;"> 
     <p>'.$reply.'</p> 
    </div> 
</body></html>'; 

$sendMail = mail($adminMail, $topic, $message, $headers); 

的index.php(形式)

<form method="GET" enctype="text/plain" accept-charset="utf-8"> 
<div class="banner-subtitle">Resposta:</div> 
<textarea name="reply" id="quizz-reply" style="width:255px;"></textarea> 
<div class="banner-subtitle">%NAME%:</div> 
<input type="text" id="quizz-name" class="required" name="name" style="width:255px;"/> 

<div class="banner-subtitle">%PHONE%:</div> 
<input type="text" id="quizz-phone" class="required" name="name" style="width:255px;"/> 
<div class="banner-subtitle">%EMAIL%:</div> 
<input type="text" id="quizz-email" class="required" name="email" style="width:255px;"/> 
<div class="banner-subtitle">%SECURITY_CODE%:</div> 
<img style="float:left; margin-right:10px" src="ajax.php?action=generateCaptcha&width=80&height=20&cell=security_code2"/><input type="text" id="quizz-security-code" class="required" name="security_code2" style="width:80px; float:left"/> 
<input type="button" value="%SUBMIT%" onclick="sendOpinion()" style="margin-top: 1px"/> 
</form> 

Ajax請求

$.ajax({ 
        url:'ajax.php?action=sendOpinion&name='+name+'&email='+email+'&phone='+phone+'&passport='+passport+'&reply='+reply, 
        success: function(data) 
        { 
          //alert(data); 
          $('#quizz-name').val(''); 
          $('#quizz-email').val(''); 
          $('#quizz-phone').val(''); 
          $('#quizz-passport').val(''); 
          $('#quizz-reply').val(''); 
        } 
       }); 

任何人都可以請幫助?我不明白我在做什麼錯...

回答

2

聲明您的網站的編碼;在<head>標籤補充一點:

<meta http-equiv="content-type" content="text/html;charset=UTF-8" /> 

BTW你應該逃脫你的變量,即使在電子郵件:

Nome: <b>'.htmlspecialchars($name, ENT_QUOTES, 'UTF-8').'</b><br /> 

而且在URL中太:

url:'ajax.php?action=sendOpinion&name='+encodeURIComponent(name)+'... 
+0

由於使用encodeURIComponent失蹤和解決這個問題。 – jribeiro