473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

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
6 14789
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
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
On a second thought, it's probably easier if you just dump the text
into a hidden field.

Mar 22 '06 #4
> 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

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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Barry Olly | last post by:
Hi, I'm working on a mini content management system and need help with dealing with special characters. The input are taken from html form which are then stored into a varchar column in...
14
by: Michael Levin | last post by:
I've got the following problem. I'm a biologist and I have a device at work which monitors my frog habitat. The device has a bunch of sensors, and runs an embedded html server with some java...
4
by: VK | last post by:
09/30/03 Phil Powell posted his "Radio buttons do not appear checked" question. This question led to a long discussion about the naming rules applying to variables, objects, methods and properties...
9
by: Robby Bankston | last post by:
I'm working on some code and am running into brick walls. I'm trying to write out Javascript with Javascript and I've read the clj Meta FAQ and didn't see the answer, read many similar posts (with...
3
by: dalei | last post by:
My question is presented more clearly in following web page: http://www.pinyinology.com/signs2.html <html> HTML entities display outside script tags: a&sup1;, a&sup2;, a&sup3;, a⁴ But...
2
by: Barney | last post by:
I have to convert a Java app to .NET, i found and used the conversion tool which converts java to C#, but it only converted about 10% of it, how hard would it be to re-write the java app and keep...
3
by: Stefan Mueller | last post by:
Because the characters ', ", ... make troubles in my input boxes I use the following PHP command while generating the page htmlentities($my_var, ENT_QUOTES); However, if I use in my java scripts...
1
by: Robert Dodier | last post by:
Hello, Sorry for asking what must be a FAQ, but I wasn't able to find the answer. I have an XML document fragment which I want to store as a text string. I want a function to convert any XML...
11
by: Nathan Sokalski | last post by:
I add several JavaScript events (onchange, onkeypress, etc.) to Controls using the Add method of the Attributes collection. However, if the JavaScript code contains certain characters, such as & or...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.