By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,407 Members | 2,842 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,407 IT Pros & Developers. It's quick & easy.

How to convert HTML special characters to the real characters with a Java script

P: n/a
I read data (e.g. "') from my MySQL database which I'd like to
show in an input box.

<?php
$mysql_data = "\"'";
$html_data = addslashes(htmlentities($mysql_data, ENT_QUOTES));

echo "<script type = 'text/javascript'>";
echo "function set_old_data() {";
echo "my_form.input1.value = var_old_data;";
echo "}";
echo "var_old_data = '" . $html_data . "';";
echo "</script>";

echo "<body>";
echo "<form name = 'my_form' action = '' method = 'post' accept-charset
= 'iso-8859-1'>";
echo "<input type = 'text' name = 'input1' value = '" . $html_data .
"'>";
echo "<input type = 'button' value = 'Old Data' onClick =
'set_old_data()'>";
echo "</form>";
echo "</body>";
?>

The command
echo "<input type = 'text' name = 'input1' value = '" . $html_data . "'>";
shows my data "' in the input box perfect.

But if I click on the button 'Old Data' the Java script function
'set_old_data' shows in the input box
&auml;&ouml;&uuml;&Auml;&Ouml;&Uuml;&ccedil;&eacut e;&agrave;&egrave;&quot;'
instead of
"'

Therefore I need a Java script function with translates
&auml;&ouml;&uuml;&Auml;&Ouml;&Uuml;&ccedil;&eacut e;&agrave;&egrave;&quot;'
to
"'

In PHP I could do that with the function
html_entity_decode()

But how can I do it with a Java script?
Stefan

PS: html_entity_decode() is the opposite of htmlentities(). It converts all
HTML entities to their applicable characters from string.
Mar 21 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Stefan Mueller wrote:
I read data (e.g. "') from my MySQL database which I'd like to
show in an input box.

<?php
$mysql_data = "\"'";
$html_data = addslashes(htmlentities($mysql_data, ENT_QUOTES));

echo "<script type = 'text/javascript'>";
echo "function set_old_data() {";
echo "my_form.input1.value = var_old_data;";
echo "}";
echo "var_old_data = '" . $html_data . "';";
echo "</script>";

echo "<body>";
echo "<form name = 'my_form' action = '' method = 'post' accept-charset
= 'iso-8859-1'>";
echo "<input type = 'text' name = 'input1' value = '" . $html_data .
"'>";
echo "<input type = 'button' value = 'Old Data' onClick =
'set_old_data()'>";
echo "</form>";
echo "</body>";
?>

The command
echo "<input type = 'text' name = 'input1' value = '" . $html_data . "'>";
shows my data "' in the input box perfect.

But if I click on the button 'Old Data' the Java script function
'set_old_data' shows in the input box
&auml;&ouml;&uuml;&Auml;&Ouml;&Uuml;&ccedil;&eacut e;&agrave;&egrave;&quot;'
instead of
"'

Therefore I need a Java script function with translates
&auml;&ouml;&uuml;&Auml;&Ouml;&Uuml;&ccedil;&eacut e;&agrave;&egrave;&quot;'
to
"'

In PHP I could do that with the function
html_entity_decode()

But how can I do it with a Java script?
Stefan

PS: html_entity_decode() is the opposite of htmlentities(). It converts all
HTML entities to their applicable characters from string.


Uh, maybe ask in a Javascript newsgroup?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 21 '06 #2

P: n/a
Stefan Mueller wrote:
Therefore I need a Java script function with translates
&auml;&ouml;&uuml;&Auml;&Ouml;&Uuml;&ccedil;&eacut e;&agrave;&egrave;&quot;'
to
"'

In PHP I could do that with the function
html_entity_decode()

But how can I do it with a Java script?
Stefan

PS: html_entity_decode() is the opposite of htmlentities(). It converts all
HTML entities to their applicable characters from string.


This relies on IE extensions but you can it's possible to make it
standard compatible.

function html_entity_decode(s) {
var span = document.createElement('SPAN');
span.innerHTML = s;
return span.innerText;
}

Mar 22 '06 #3

P: n/a
On a second thought, it's probably easier if you just dump the text
into a hidden field.

Mar 22 '06 #4

P: n/a
> function html_entity_decode(s) {
var span = document.createElement('SPAN');
span.innerHTML = s;
return span.innerText;
}


Yea, this is exactly what I'm looking for. But like you mentioned it only
works for IE and not for Mozilla, Opera, Safari, ...

Stefan

Mar 22 '06 #5

P: n/a

Stefan Mueller wrote:
function html_entity_decode(s) {
var span = document.createElement('SPAN');
span.innerHTML = s;
return span.innerText;
}


Yea, this is exactly what I'm looking for. But like you mentioned it only
works for IE and not for Mozilla, Opera, Safari, ...

Stefan


Most browsers support innerHTML, even though it's non-standard, because
it's so convinent. innerText is supported only by IE as far as I know.
In the other browsers, you can get the plain text from
span.firstChild.nodeValue.

As I said in the follow-up, it's easier to store the original value in
a hidden field parallel to the input field. Or better yet, use a onload
handler to capture the initial values of every fields. Something like:

function saveOriginalValues() {
var inputs = document.getElementsByTagName('INPUT');
for(var i = 0; i < inputs.length; i++) {
var input = inputs[i];
if(input.type == 'text') {
originalValues[input.name] = input.value;
}
}
}

That gives you something that you can reuse across multiple forms.

Mar 22 '06 #6

P: n/a
> As I said in the follow-up, it's easier to store the original value in
a hidden field parallel to the input field. Or better yet, use a onload
handler to capture the initial values of every fields. Something like:


First of all thanks a lot for your solutions.

Yea, I've also thought about your solution (hidden fields). However, I've a
table with hundreds of fields. Today, to sort this table takes already some
time. But if I add for each field a hidden field the sorting will take to
much time. Therefore I try to find a solution to get the real characters (,
, , ...) from the HTML characters (&auml;&ouml;&uuml;, ...).
In the worst case I've to write my own function (search for '&auml;' and
replace it with '', then seach for '&ouml;' and replace it with '', ...)

Stefan
Mar 22 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.