tags:

views:

98

answers:

3

i have following data to save in database using php

my data is : 12 ÷ 5

i have following code

       $.ajax({
   type: "POST",
    url: "add_proc.php",
    data: "topic="+ encodeURIComponent(field1)

when i check value of field1 in firebug its same (12 ÷ 5)

but when value save in database is 12 ÷ 5

         $topic=mysql_escape_string($_POST['topic']);
          echo($topic);

even if echo the value of variable after post i get 12 ÷ 5

i can't figure out where is problem ?

thanks

A: 

Make sure the content encoding of your input HTML page is UTF-8. That way, PHP will know that the content that it is receiving is in UTF-8.

You can do this by embedding the following in the head of your document:

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

Update: Also make sure that the HTML that you're generating (echo'ing from) also reports UTF-8 as the content encoding. Once you get the echo working, the next step is to ensure that you're DB is also expecting/storing UTF-8.

Ates Goral
You'll also want to make sure the database table is using UTF8 encoding as well.
Jason
Note that this won't work if the server is already specifying a different character encoding in an HTTP header; the header takes precedence, as per the HTML 4.01 spec: http://www.w3.org/TR/html401/charset.html#h-5.2.2. More on this from the WHAT-WG: http://blog.whatwg.org/the-road-to-html-5-character-encoding
NickFitz
my php page add_proc.php has<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">but after echo i can see same result.
air
+1  A: 

This is definitely an encoding problem. Here's why.

The division sign (U+00F7) is encoded in UTF-8 as 2 bytes, 0xC3 and 0xB7.

But in ISO-8859-1 (and many other encodings) those bytes individually are two separate characters: U+00C3 and U+00B7 respectively.

So somewhere in the process your UTF-8 encoded division sign is being decoded as some other encoding (ISO-8859-1 most likely).

Check out the PHP UTF-8 Cheatsheet as a start. If you work through all those steps and still have a problem, let me know and I'll see what I can do to help.

Peter Bailey
+2  A: 

The ÷ is being UTF-8 encoded -- http://www.fileformat.info/info/unicode/char/00f7/

#pseudo
utf8('÷')    => 'Ã' + '·'
utf8(0x00F7) => 0xC3, 0xB7

You can use utf8_decode (I'm guessing the encoding is from $_POST):

$topic = mysql_escape_string(utf8_decode($_POST['topic']));

Be sure that your database/tables have Unicode/UTF-8 support.

Alternatively, leave it encoded and just echo as is with UTF-8 encoding in the page (as Ates Goral suggested) so the browser will handle the decoding for you.

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Jonathan Lonowski
thanks, it works
air
I don't know if I'd recommend `utf8_decode()`. That doesn't really address the problem, but rather skirts around it by changing the encoding to ISO-8859-1, which then means you're basically ditching UTF-8 support.
Peter Bailey