Process Chinese Characters in erlyweb

Source: Internet
Author: User

Reprinted: http://blog.csdn.net/minyskirt/article/details/5291494

In the old version of Erlang, Unicode (utf8) characters are not supported, but fortunately, it is supported in R13. If you are interested, you can find an example at http://erlang-china.org/misc/erlang_r13_unicode.html.

 

Naturally, since the previous Erlang version does not provide Unicode support, it is very complicated to use erlyweb to process Chinese character input when developing a web application, although Chinese characters can be read from the MySQL database.

 

Here are some notes for processing Chinese characters using erlyweb:

1) set the character encoding of the MySQL database to Unicode.

2) The character encoding of each HTML page must be set to UTF-8, add <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"> to the

3) To use erlydb to link to the MySQL database, you need to add character encoding parameters, such as erlydb: Start (MySQL, [{hostname, "localhost"}, {username, "root" },{ password, "123456" },{ database, "blog" },{ encoding, utf8}])., if you directly use the MySQL driver of Erlang to connect to the database, you can: mysql: start_link (P1, "localhost", 3306, "root", "123456", "blog", undefined, utf8 ).

4) when writing Chinese characters into the database, the list of Chinese characters is converted to binary. For example, if the Chinese character "Wang" is obtained using yaws_api: parse_post, the corresponding value is a list [231,142,139], and the value in the list is exactly the binary of the character "Wang, therefore, you need to use the iolist_to_binary/1 or list_to_binary/1 function to convert it to binary <231,142,139>,

Finally, assign the converted binary value to the corresponding field.

 

The following code is a simple function to add data to a database table. For details, refer:

1), insert_controller.erl

-Module (insert_controller ).
-Compile (export_all ).

Index (A)->
Case yaws_arg: method (A)
'Post'->
Params = yaws_api: parse_post (),
{[Firstname, lastname, email], errors} = erlyweb_forms: Validate (Params, ["firstname", "lastname", "email"], fun validate/2 ),
If errors =/= []->
{Data, {firstname, lastname, email, errors }};
True->
Register_usr (firstname, lastname, email ),
{EWR, show}
End;
_->
{Data, {[], [], [], []}
End.

Validate (name, Val)->
Case name
"Firstname"->
If Val = []->
{Error, {missing_field, "firstname "}};
True->
OK
End;
"Lastname"->
If Val = []->
{Error, {missing_field, "lastname "}};
True->
OK
End;
"Email"->
If Val = []->
{Error, {missing_field, "email "}};
True->
OK
End;
_->
OK
End.

Register_usr (firstname, lastname, email)->
First_name =Iolist_to_binary(Firstname ),
Last_name =Iolist_to_binary(Lastname ),
% IO: Format ("~ P ~ N ", [firstname]),
% IO: Format ("~ P ~ N ", [first_name]),
Author = Author: new_with ([{first_name, first_name}, {last_name, last_name}, {email, email}]),
Author: Save (author ).

 

2), insert_view.et

<% @ Index ({firstname, lastname, email, errs1}) %>
<% Test_messages: show_errs (errs1) %>
<Form action = "insert" method = "Post">
<Div> firstname </div>
<Div> <input name = "firstname" id = "firstname" value = "<% firstname %>"/> </div>
<Div> lastname </div>
<Div> <input name = "lastname" id = "lastname" value = "<% lastname %>"/> </div>
<Div> email </div>
<Div> <input name = "email" id = "email" value = "<% email %>"/> </div>
<Div> <input type = "Submit" value = "insert"/> </div>
</Form>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.