Jonas
Jonas

Reputation: 128807

How to post non-ascii chars in a HTML from and read it using PHP?

I have created a small HTML form and saved it as an UTF-8-encoded PHP-file:

<!DOCTYPE html>
<html>
<head><title>My form</title></head>
<body>
<h1>My form</h1>
<form method="post">
<input type="text" name="myfield" /><br />
<input type="submit" value="Send" />
</form>
</body>
</html>

But if I type Öst in the text field and post it, the swedish letter Ö is stripped so I only get st when I read it with $_POST['name']. I am using CodeIgniter PHP Framework, and if I read it with $this->input->post('name'); I get the same result, only st.

Why do I get this problem? Do CodeIgniter strip my swedish letter or do I have some problem with the character encodings?

UPDATE: I have added a meta-tag <meta charset="utf-8" \> in the header and now it works.

Upvotes: 0

Views: 2078

Answers (4)

davzie
davzie

Reputation: 116

Use CodeIgniter's Convert To Entities function. It's rather awesome.

Upvotes: 0

Jakub
Jakub

Reputation: 20475

As long as you make sure that your HTML output is UTF-8 (proper header), you may use this function when outputing details in a form in order not to break the text:

form_prep()

Allows you to safely use HTML and characters such as quotes within form elements without breaking out of the form. Consider this example:

$string = 'Here is a string containing "quoted" text.';    
<input type="text" name="myform" value="$string" />

Details are here in the User Guide.

Upvotes: 1

jhavrda
jhavrda

Reputation: 395

Guess here is an answer tailored for Codeigniter:

http://www.haughin.com/2010/02/23/building-utf8-compatible-codeigniter-applications/

Basically, you'll have to create new form_helper.php file with modified form_open and form_prep functions. These modifications will allow posting of UTF-8 characters.

Upvotes: 2

Andrew Jackman
Andrew Jackman

Reputation: 13966

Check that all the php files (controllers and what not) are also UTF-8

Upvotes: 0

Related Questions