8

I'm working on a multilingual site with CodeIgniter. There is a form that posts data to controller, but $_POST is empty when I start to use Turkish characters like öçüÜĞ etc.

I set the charset to:

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

Form:

<form action="translations/save" method="post" accept-charset="utf-8">
    <textarea rows="6" cols="60" id="editor_tr" name="editor_tr">Türkçe</textarea>
</form>

$_POST and $this->input->post('editor_tr') returns empty, but I can see the raw post with file_get_contents("php://input").

This one works OK in a normal PHP test, but doesn't work with CodeIgniter. Perhaps my .htaccess file is causing the issue, but dunno.

Any help is much appreciated.

UPDATE: Here is the output for var_dump as requested.

var_dump($_POST) - Without Turkish chars

array(3) { ["id"]=> string(12) "news8titleID" ["editor_tr"]=> string(13) "turkish value" ["editor_en"]=> string(13) "english value" }

var_dump($_POST) - With Turkish chars (The input was: Türkçe karakter, but it doesn't show up in the $_POST)

array(3) { ["id"]=> string(12) "news8titleID" ["editor_tr"]=> string(0) "" ["editor_en"]=> string(13) "english value" }

UPDATE 2: When debugging, I have found out that system.core.Input class cleans the input data on _clean_input_data function.

// Clean UTF-8 if supported
if (UTF8_ENABLED === TRUE)
{
    $str = $this->uni->clean_string($str);
}

So, before the $_POST has reached to my controller, the editor_tr value is already cleaned by system.core.Utf8 class in this function:

function clean_string($str)
{
    if ($this->_is_ascii($str) === FALSE)
    {
        $str = @iconv('UTF-8', 'UTF-8//IGNORE', $str);
    }

    return $str;
}
Montag451
  • 1,168
  • 3
  • 14
  • 30
Ozay
  • 443
  • 1
  • 6
  • 12
  • have you tried mbstring? and the associated fix for [codeigniter](http://www.haughin.com/2010/02/23/building-utf8-compatible-codeigniter-applications/) – Dr.J Apr 25 '11 at 23:28
  • `My .htaccess file perhaps is causing the issue, but dunno.` Consider supplying it then ? – Khez Apr 25 '11 at 23:30
  • @Madmartigan Sorry, that was a typo. – Ozay Apr 26 '11 at 08:11
  • @Dr.J I implemented mbstring to my codeigniter after your comment, but still no luck. It ignores the element in the $_POST when it has utf-8 chars. – Ozay Apr 26 '11 at 08:13
  • @Khez Here is my .htaccess file:`RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]` – Ozay Apr 26 '11 at 08:14
  • @user What is your output with `var_dump($_POST)` when you submit the string `abc`, and then when you submit the string `Türkçe`? If you add it to the question it will help. I'm kind of interested in this, but I also suspect it may be a simple error on your part, and that this has nothing to do with Codeigniter. I'm also 99.9% certain that .htaccess has nothing to do with it. In the future make sure to add any relevant info *to your question's content* and not in a comment. – Wesley Murch Apr 26 '11 at 08:23
  • @Madmartigan. Thanks for the feedback and looking into this. I have edited my post and included the `var_dump` output for both. – Ozay Apr 26 '11 at 08:42
  • @user Please add which version of Codeigniter you are using as well. (1.7.2, 2.0.1, 2.0.2 etc) If you aren't sure just print the constant `CI_VERSION`. And please confirm: You have `application/config/config.php` charset as UTF-8, correct? – Wesley Murch Apr 26 '11 at 08:51
  • @Madmartigan I'm using CI 2.0, yes, that's correct. I use UTF-8 in my config file: `$config['charset'] = 'UTF-8';` – Ozay Apr 26 '11 at 08:59
  • OK, try one more test please: Try some of these characters `♥ ™ ⅓ “ ”` and we'll see if it's a UTF-8 issue or problem with accented Turkish characters only. It's a long shot but I'm almost out of ideas. – Wesley Murch Apr 26 '11 at 09:03
  • It didn't like these characters either. I edited my post above for more info. – Ozay Apr 26 '11 at 09:06
  • Where do the characters in the URL come from? Are you entering them directly into the browser? Does CI have a global "character set" setting and what is it set to? – Pekka Apr 26 '11 at 09:11
  • @Pekka I have a form and 2 textareas inside. These characters are coming when the user submits the form. The global charset should be utf-8. – Ozay Apr 26 '11 at 09:18
  • And the form is in a UTF-8 encoded page? The browser shows "UTF-8" in the encoding menu? You have tried removing the preset default and typing in `Türkçe` by hand? – Pekka Apr 26 '11 at 09:20
  • @user Just want to confirm that I am getting the same issue in CI 2.0.2 as well as 2.0.1 even after manually "disabling" the UTF8 system library. If I submit HelloöçüÜĞ, only Hello is sent. – Wesley Murch Apr 26 '11 at 09:23
  • @Pekka Yes, the browser shows UTF-8 and I type the chars by ahnd, but still no luck. @Madmartigan Not sure if this is CI bug, I have no problem with displaying the UTF-8, but with the input. – Ozay Apr 26 '11 at 09:34
  • @user: I think it is indeed a CI bug, I just tried this with fresh default installs of 1.7.2 and 2.0 and had different results. I also `var_dump($_POST)` in the Input class constructor (v2.0.2) and was able to see all the correct characters. This has now consumed my attention and I want to find out what the cause of this is because it is affecting my work as well. – Wesley Murch Apr 26 '11 at 09:42
  • @Madmartigan Thanks for this valuable information and time you spent on this. Since I'm still in the development phase, I will upgrade to 2.0.2 and test what happens. – Ozay Apr 26 '11 at 09:45
  • Very strange news: I re-checked versions 1.7.2 to 2.0.2 (5 total versions, default installations) and now I **cannot** reproduce the problem, even though I could before just moments ago! Even stranger: I tried *again* these same characters in your example in my CMS running 2.0.2, and now suddenly they work! I did NOT change anything. Very confused and curious about this issue, maybe it is not CI related? – Wesley Murch Apr 26 '11 at 10:10
  • One more thing: Make sure you aren't running any validation or prepping rules on the field. Things like `url_title()` will strip those characters. – Wesley Murch Apr 26 '11 at 10:36
  • @Madmartigan I'm not running any validation rules. I will try to test on a fresh install and give feedback here. – Ozay Apr 27 '11 at 09:04
  • It seems to be specific to CI running in PHP 5.3.x on MAMP. It does not occur in MAMP PHP 5.2.17, nor is it a problem on my CentOS LAMP server running PHP 5.3.8. I'd love to know what difference is causing the problem in MAMP. – Ade Oct 22 '11 at 23:32

4 Answers4

7

Since the comments are piling up and will probably get overlooked, I am going to post some more suggestions.

Readers please keep in mind this is dealing with $_POST data values and not display of data on a web page.

This user seems to have a similar problem:

Codeigniter seems to break $_POST of '£' character (Pound)

There is a report here on Bitbucket of similar:

https://bitbucket.org/ellislab/codeigniter-reactor/issue/214/problem-when-inserting-special-characters: Removed link: EllisLabs has closed this repo to the public

Maybe adding this to your index.php will help (probably not):

ini_set('default_charset', 'UTF-8');

Double check and make sure you aren't running any validation or prepping rules on the field. Things like url_title() will strip those characters.

Community
  • 1
  • 1
Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
  • 1
    The first link you gave solved the problem. Phew. Didn't think that MAMP & PHP would cause this, but you never know. Thanks a lot! – Ozay Apr 27 '11 at 09:13
  • That's great news, thanks for being so responsive and providing the relevant details promptly. Good luck with your project :) – Wesley Murch Apr 27 '11 at 09:16
  • Thanks Madmartigan, hope this helps others who run into same issue. – Ozay Apr 27 '11 at 09:27
  • 1
    I spent about 2 hours on this problem, would never guess that MAMP will cause such a funny problem. Thank you :) – Herr Jul 21 '11 at 08:32
6

Make sure the form tag has accept-charset:

<form method="post" action="" accept-charset="utf-8">

Then in the controller use utf8_decode() when fetching the posted values:

$value = utf8_decode($this->input->post('field_name'));
Fredrik
  • 73
  • 1
  • 4
  • This should be the accepted solution, or at least upvoted more. The solution at bitbucket provided by Ellislab (in @WesleyMurch answer) is not longer available, since Ellislab closed the repo. – Lvkz Jan 06 '15 at 23:55
1

If you don't want to use a previous PHP version in your MAMP instalation you can use:

$_REQUEST

To get the data instead of $_POST

$_REQUEST is an associative array that by default contains the contents of $_GET, $_POST and $_COOKIE.

More info: http://php.net/manual/en/reserved.variables.request.php

And that returns all the data that for some reason $_POSTis breaking !

jmserra
  • 1,296
  • 4
  • 18
  • 34
-1

This isnt an answer, but you might want to take a look and see how everything is coming in.


foreach($_POST as $key => $val)
{
     $post_data[$key] => $val;
}
print_r($post_data);

then try with CI's post


foreach($_POST as $key => $val)
{
     $post_data[$key] => $this->input->post($key);
}

print_r($post_data);
Peter
  • 2,276
  • 4
  • 32
  • 40