Kreator Kreator - 3 months ago 46
jQuery Question

Can't send form data in Cyrillic

I have a simple contact form (name, email, subject, message) which sends messages to email. I've set client-side validation with jQuery and server-side validation with PHP scripts. I've connected them with Ajax. And it works, but only when I insert data in Latin characters.

When I insert data in Cyrillic, the same form doesn't work. It doesn't send any message. I've searched Internet for similar topics, and YT for some tutorials, but after 4-5 days I still got nothing. Maybe it's something like putting some codes in .htaccess file, but I realy don't know.

Answer

This is a basic encoding issue. Make sure your form is set up to POST as UTF-8:

<form method="post" action="/your/url/" accept-charset="UTF-8">
</form>

Also make sure your page is delivered using the correct headers:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Then make sure you are sending the email with correct UT8 headers. I found this code:

function mail_utf8($to, $subject = '(No subject)', $message = '', $header = '') {
  $header_ = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' . "\r\n";
  mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_ . $header);
}

Source: http://bitprison.net/php_mail_utf-8_subject_and_message

Comments