简体   繁体   中英

Special characters escaping with JS and PHP

my application geting Text from a input field an post it over ajax to a php file for saving it to db.

var title = encodeURIComponent($('#title').val());

if I escape() the title it is all OK but i have Problems with "+" character. So i use the encodeURIComponent().

Now i habe a Problem with german special characters like "ö" "ä" "ü" they will be displayed like a crypdet something....

Have some an idea how can i solve this problem?

Thx

I suppose this has to do with encoding : your HTML page might be using UTF-8, and the special characters are encoded like this :

>>> encodeURIComponent('ö');
"%C3%B6"

When your PHP page receives this, it has to know it's UTF-8, and deal with it as UTF-8 -- which means that everything on the server-side has to work with UTF-8 :

  • PHP code must use functions that can work with multi-byte characters
  • The database (db, tables, columns, ...) must use UTF-8 for storing data
  • When generating HTML pages, you need to indicate it's UTF-8 too, ...


For instance, if you are using var_dump() on the PHP side to display what's been sent from the client, don't forget to indicate that the generated page is in UTF-8, with something like this :

header('Content-type: text/html; charset=UTF-8');

Else, the browser will use it's default charset -- which is not necessarily the right one , and possibly display garbage.

You might use escape("AbcÄüö") and you would get "Abc%C4%FC%F6"

In php you could then use urldecode($myValue) to get "AbcÄüö" again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM