encodeURIComponent() and UTF-8 when sending Form textarea via AJAX

3k Views Asked by At

I am sending data to a PHP backend file via AJAX method POST. The data comes from a textarea, whose form has the next atribbute accept-charset="UTF-8". The AJAX function sends data as http[act].send("title=" + encodeURIComponent(field.value)...) and my PHP backend file begins with header('Content-Type:text/html; charset=UTF-8');. Database ACCEPT NAMES and FIELDS are all set to UTF-8 (never had problems with this stuff).

I think I should decode in the PHP backend the encodeURIComponent() func. But how?

Edit (sorry):

Fibra dietética is inserted in DB as fibra dietética
Acompañada is inserted as Acompañada

...and so on.

3

There are 3 best solutions below

0
On BEST ANSWER

Mistake. I was using htmlentities() (PHP) on the string before doing INSERT. A good alternative was using strip_tags() (PHP) to avoid "HTML/Js/... injection". Thanks to everyone for helping. i hope this will be useful for someone.

5
On

use rawurldecode($value) see here http://www.php.net/manual/en/function.rawurldecode.php

0
On

peacemaker is right in Ajax, use

encodeURIComponent()

then in php, use

rawurldecode()

Example in Ajax

abc=encodeURIComponent(xxx)

then in php,

$varName = rawurldecode($_POST['abc'])