g5wx
g5wx

Reputation: 720

Ajax contact form and UTF-8

i'm using ajax contact form, downloaded from: http://youhack.me/2010/07/22/create-a-fancy-contact-form-with-css-3-and-jquery/

Everything works ok except UTF as i can't use cyrilic symbols when submitting.

The php:

$name = $_POST['name']; // contain name of person
$email = $_POST['email']; // Email address of sender 
$web = $_POST['web']; // Your website URL
$body = $_POST['text']; // Your message 
$receiver = "[email protected]" ; // hardcorde your email address here - This is the email address that all your feedbacks will be sent to 

if (!empty($name) & !empty($email) && !empty($body)) {
    $body = "Name: {$name}\n\nSubject: {$web}\n\nMessage: {$body}";
    $send = mail($receiver, 'Contact from domain.com', $body, "From: {$email}");
    if ($send) {
        echo 'true'; //if everything is ok,always return true , else ajax submission won't work
    }
}

It uses jquery.validationEngine-en for validation. My html already has "Content-Type" content="text/html; charset=utf-8" in header.

I'm new to php and jquery, so i would appriciate some guidance to make UTF-8 work when submitting. Thanks :)

Edit: When i try to use cyrilic chars (čšćđ) on a required field i get ajax input error "Please use letters only". If i submit the form with cyrilic chars on a non-required field, i receive and email, all letters show ok except cyrilic, which are like this: Å¡.

Edit 2: When i set the recipient to gmail (webmail), cyrilic chars show up ok, except in one field, where Ajax doesnt let me use them (regex from Reinder answer).

When i set recipient in outlook (local) and submit the form, none of the cyrilic chars don't show up ok, example: ÄĹĄ oÄa ĹĄ ÄŽŠÄÄ

SOLVED Thanks to Reinder for guide and David! Will solve it today :)

Upvotes: 2

Views: 1321

Answers (2)

Reinder Wit
Reinder Wit

Reputation: 6615

having looked at the plugin you're using, I think this has to do with the validation regex inside jquery.validationEngine-en.js

when the validation is set to 'onlyLetter' it will check using

/^[a-zA-Z\ \']+$/

and none of your characters čšćđ are allowed here...

you need to create a language validation javascript for the language you're using and change that regular expression. For example, have a look at this post

The next thing is to check the encoding of your PHP file and your headers. Place this at the top of your PHP code

 header("Content-type: text/html; charset=utf-8");

Check if the values are correctly displayed when just outputting them in PHP, like so:

echo $name;

If they are correctly displayed in the browser and it's just the email that's incorrectly displaying the characters, then you need to pass an encoding header to the email too

example:

$headers = "From: $name <$email>\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\n";
$body = "Name: {$name}\n\nSubject: {$web}\n\nMessage: {$body}";
$send = mail($receiver, 'Contact from domain.com', $body, $headers);

have a look at the mail function on the PHP.NET website

Upvotes: 2

David John Welsh
David John Welsh

Reputation: 1569

Rather than use the default PHP mail() function, I've found this come in handy when working with Japanese:

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

Upvotes: 1

Related Questions