1

I have problem when post data in php that contain special character:

String: ? < > ' - \" `´& % ‰ € ® 2011

that always display

String: ? < > ' - " `´& % ‰ € ® 2011

i changed form attribute be:

<form  id="frmBlog"method="post" enctype="multipart/form-data">

and also i change html with meta attribute:

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

but the result still same. anyone can help to fix this problem. thank you

my simple code:

<?php
if(isset($_POST['submit'])){
    echo $_POST['content'];
    exit;
}

$content = "String: ? < > ' - \" ´& % ‰ € ® 2011";
?>

<html lang="en">
  <head>
    <title>Test</title>
     <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  </head>
  <body>
    <form method="post">
        <textarea name="content" rows="20"><?=
        $content
        ?></textarea>

        <input type="submit" name="submit" value="submit" />
    </form>
  </body>
</html>

output:

String: ? < > ' - " ´& % ‰ € ® 2011
her03
  • 152
  • 4
  • 13
  • Have you checked whether your browser is actually following the charset instruction? – Phylogenesis Jul 29 '15 at 08:53
  • Have you also checked the encoding of your source files? – Havelock Jul 29 '15 at 08:54
  • Your source .php file must be also saved with utf-8 encoding. – macl Jul 29 '15 at 09:00
  • The encoding of the source file should make no difference. The output you're seeing is your browser/client attempting to decode a UTF-8 encoded string as ISO-8859-1 (or other single-byte character set). – Phylogenesis Jul 29 '15 at 09:05
  • when display in form i try to: $['content'] "String: ? < > ' - \" `´& % ‰ € ® 2011"; when post data echo $_POST['content']; this display value: String: ? < > ' - " `´& % ‰ € ® 2011 – her03 Jul 29 '15 at 09:05
  • 1
    [Handling Unicode Front To Back In A Web App](http://kunststube.net/frontback/) – deceze Jul 29 '15 at 09:40

1 Answers1

2

If your string goes through a database, that could be the problem. Try to change database encoding options. (This worked for me once)

Otherwise, try to use utf8_encode() and utf8_decode() and see if you still have the problem. And if you still do, then the problem is probably in your form, or the way it is submitted (in my opinion).

EDIT:

Your problem was that when you echo the content, the charset is not yet defined, and that causes the problem. You should echo after the <head> , like this:

<html lang='en'>
  <head>
    <title>Test</title>
     <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  </head>
  <body>

    <?php
    if(isset($_POST['submit'])){
        echo $_POST['content'];
        exit;
    }

    $content = "String: ? < > ' - \" ´& % ‰ € ® 2011";
    ?>


    <form method="post" action="post.php">
        <textarea name="content" rows="20"><?php
            echo $content;
        ?></textarea>

        <input type="submit" name="submit" value="submit" />
    </form>
  </body>
</html>
Sorin C
  • 984
  • 10
  • 8