473,509 Members | 2,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14804
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
17175
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
2993
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
8386
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
2858
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
2662
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
28805
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
12939
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
7676
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
2758
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...
0
7237
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
7137
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
7416
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...
1
7073
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
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5062
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...
0
4732
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3207
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1571
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.