php - Form encoding problem in different browsers

253

I'm trying to create and submit a form for a website and send a mail after all fields are submitted. The data is submitted through ajax calls.

The problem is that my site users work in UTF-8 and if I submit the form with Internet Explorer the info (from the form) is not sent in UTF-8 unless i do utf8_encode(). But if i send the email using the ut8_encode function Firefox and Chrome stops working...

Some notes: - The php file containing the form has header in UTF-8 - meta-tag UTF-8 - In the ajax.php (file where the form is submitted) also has header in UTF-8 - Email headers are received in UTF-8 charset (independent of the browser)

Code: 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 >
  <h1 >Quizz</h1>
  <div >
    Nome: <b>'.$name.'</b><br />
    Telefone: <b>'.$phone.'</b><br />
    E-mail: <a href="mailto: '.$email.'"><b>'.$email.'</b></a><br />
  </div>
  <div >
    <p>'.$reply.'</p>
  </div>
</body></html>';

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

index.php (form)

Ajax Request

$.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('');
                    }
                });

Can anyone please help? I can't understand what I'm doing wrong...

817

Answer

Solution:

Declare the encoding of your website; add this in your<head> tag:

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

BTW you should escape your variables, even in emails:

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

And in URLs too:

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

People are also looking for solutions to the problem: php - convert hours : mins to the corresponding epoch time that the value would be today

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.