keyongtech


  keyongtech > php > 03/2006

 #1  
03-21-06, 06:26 PM
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;&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.
 #2  
03-21-06, 07:39 PM
Jerry Stuckle
Stefan Mueller wrote:
[..]
> 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?
 #3  
03-22-06, 12:36 AM
Chung Leong
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;
}
 #4  
03-22-06, 12:38 AM
Chung Leong
On a second thought, it's probably easier if you just dump the text
into a hidden field.
 #5  
03-22-06, 04:20 AM
Stefan Mueller
> 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
 #6  
03-22-06, 06:09 AM
Chung Leong
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.
 #7  
03-22-06, 11:46 AM
Stefan Mueller
> 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
Similar Threads
Convert special characters from HTML to utf-8 or iso-8859-1

Dear all, is there a gem available that will transform things like "—" into a long dash, "'" into an apostrophe and so on ? I think someone posted about this about a week...

Special characters using html &..;

Whenever I write norwegian characters like æ, ø and å, FrontPage no longer translates them into html-codes æ ø and å. In fact, if I code it like this directly into the html...

How to convert HTML entities to their applicable characters with a java script

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...

Special characters in java, oracle and html

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...

Special characters in java, oracle and html

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...


All times are GMT. The time now is 09:50 AM. | Privacy Policy