views:

58

answers:

3

Hello all,

We are doing an ajax call to retrieve from database. Since our clients may use different languages we encode everything into unicode to store in the database (saves worrying about collations and such). Now when we fetch such content to be displayed in an input text field it is displaying the unicode codes. Checked the HTML 4 documentation for input and value is a CDATA which tells me those unicode should be displayed as their character.
From the screen shot attached you can see this is not the case, Im wondering if there is a way to "force" this behavior

alt text

+1  A: 

Since our clients may use different languages we encode everything into ascii to store in the database (saves worrying about collations and such).

IMHO storing html entities into the database is a very bad approach. I would strongly recommend you using UTF-8 encoding everywhere. This is what will save you from worrying about collations and such.

Darin Dimitrov
My apologies, that is not ASCII, thats unicode.
Purefan
+1  A: 

You're passing a JavaScript string full of &#...; numeric character references. JavaScript strings are not HTML-encoded, so your code really does make a JS string containing an ampersand, a hash, and so on.

When you set it as an input's DOM value (val()) naturally those ampersands will still be there. DOM properties are plain strings, not markup. You only need to HTML-encode strings in JavaScript if you intend to make markup out of them for use with innerHTML/html().

So the PHP should not HTML-encode content that isn't going be to inserted into HTML. Use json_encode() to create JavaScript string literals:

$('#js_global_status_input').val(<?php echo json_encode($status_value); ?>);
bobince
Thank you very much for your time and suggestion, it however did not work on my end, here is the ajax response: $("#js_status_input").val("מה בראש שלך?"); that is using json_encode
Purefan
You are still passing in ampersands and hashes and that. Don't do that, they have no meaning in anything but markup. `val()` is not markup, you need to pass a normal, unencoded string: `val('מה בראש שלך')`, or, if escaped for ASCII-safety in a JSON string (which `json_encode` will do by default), `val("\u05de\u05d4 \u05d1\u05e8\u05d0\u05e9 \u05e9\u05dc\u05da")`. You need to get your server-side script to stop HTML-encoding the value it is spitting out.
bobince
If you actually literally have `מה...` in your database content, then you've got big problems. Whilst you can try to fix this brokenness by calling `html_entity_decode($s, ENT_QUOTES, 'utf8')` in PHP, having HTML-encoded text in your database is a sure sign that your application is doing something very wrong, like HTML-encoding all input, instead of encoding it at the output stage. Database content should be kept in raw text form at all times.
bobince
A: 

Found the fix in this other thread. Thanks a lot to everyone who contributed

Purefan