Search code examples
phpajaxpostutf-8encodeuricomponent

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


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.


Solution

  • 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.