`$('#form').serialize()` messes up UTF-8 characters

11,200

Solution 1

Jquery serialize() serializes as UTF-8. So á becomes the correct UTF-8 encoding %c3%a1. Try using the $_REQUEST as that is already decoded as per the php documentation. here

Solution 2

The final step you need to do is to decode in your PHP file like this:

$usuario=utf8_decode($_POST['nombre']);

I use to combine utf8_decode() with htmlspecialchars() before sending datas to the database:

$nombres = utf8_decode(htmlspecialchars($_POST['nombres']));

Share:
11,200
lisovaccaro
Author by

lisovaccaro

Updated on June 04, 2022

Comments

  • lisovaccaro
    lisovaccaro almost 2 years

    I'm inserting a form with AJAX and I'm using: $('#form').serialize() to get all the input and send it to insert.php.

    The problem is that characters like á become %A9 and such.

    How can I prevent this from happening before sending it or how get the correct characters when I retrieve it through $_POST so I can insert them correctly to my database?

    EDIT -----

    Btw: Same Form with no AJAX, just action="POST" to the SAME insert.php inserts correctly, so the problem is solely with serialize() messing the HTML.

  • Vörös Imi
    Vörös Imi over 5 years
    urldecode($_POST['data']) before inserting into database solved it for me!