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

S

Stefan Mueller

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;&eacute;&agrave;&egrave;&quot;'
instead of
äöüÄÖÜçéàè"'

Therefore I need a Java script function with translates
&auml;&ouml;&uuml;&Auml;&Ouml;&Uuml;&ccedil;&eacute;&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.
 
T

Toby Inkster

Stefan said:
echo "var_old_data = '" . $html_data . "';";

At a guess... you're pasting $html_data into a section of Javascript --
i.e. you're pasting HTML data into something that isn't HTML. So this data
shouldn't be escaped with htmlentities().

(More technically, the stuff between <script> and </script> is CDATA, so
you don't need to do any HTML-style escaping -- you just need to avoid
using '</'.)
 
N

nice.guy.nige

While the city slept, Stefan Mueller ([email protected]) feverishly
typed...

[html entities, php & js]
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;&eacute;&agrave;&egrave;&quot;&#
039;
instead of äöüÄÖÜçéàè"'

Therefore I need a Java script function with translates

&auml;&ouml;&uuml;&Auml;&Ouml;&Uuml;&ccedil;&eacute;&agrave;&egrave;&quot;&#
039;
to äöüÄÖÜçéàè"'

You do indeed...... or do you?
In PHP I could do that with the function
html_entity_decode()

Yup, you probably could...
But how can I do it with a Java script?

Don't forget that php can be "slotted into" your html (and presumably
therefore your js code). Could you not use;

(javascript stuff...)

var content = "<? php echo(html_entity_decode($stringFullOfHtmlEntities))
?>";

(...more javascript stuff....)

Just a thought straight off the top of my head, no idea if it will even
work!

Hope that helps,
Nige
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top