weixin_33693070 2012-10-22 07:59 采纳率: 0%
浏览 37

Ajax联系表格和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 = "receiver@domain.com" ; // 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}

Subject: {$web}

Message: {$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 :)

  • 写回答

2条回答 默认 最新

  • 普通网友 2012-10-22 08:16
    关注

    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>
    ";
    $headers .= "Content-Type: text/plain; charset=UTF-8
    ";
    $body = "Name: {$name}
    
    Subject: {$web}
    
    Message: {$body}";
    $send = mail($receiver, 'Contact from domain.com', $body, $headers);
    

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

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?