Force display text from Unicode in input field [duplicate]
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, I'm wondering if there is a way to "force" this behavior.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.
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); ?>);
精彩评论