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

Discussion in 'HTML' started by Stefan Mueller, Mar 21, 2006.

  1. 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.
     
    Stefan Mueller, Mar 21, 2006
    #1
    1. Advertising

  2. Stefan Mueller

    Toby Inkster Guest

    Stefan Mueller wrote:

    > 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 '</'.)

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me ~ http://tobyinkster.co.uk/contact
     
    Toby Inkster, Mar 21, 2006
    #2
    1. Advertising

  3. While the city slept, Stefan Mueller () 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

    --
    Nigel Moss http://www.nigenet.org.uk
    Mail address will bounce. | Take the DOG. out!
    "Your mother ate my dog!", "Not all of him!"
     
    nice.guy.nige, Mar 22, 2006
    #3
  4. > var content = "<? php echo(html_entity_decode($stringFullOfHtmlEntities))
    > ?>";


    That was a super idea. It works perfect.

    Many thanks
    Stefan
     
    Stefan Mueller, Jul 23, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Barry Olly
    Replies:
    2
    Views:
    3,920
    Doyle
    Oct 20, 2003
  2. Replies:
    5
    Views:
    4,868
    Chris Head
    Sep 2, 2005
  3. Axel Etzold
    Replies:
    1
    Views:
    285
    Axel Etzold
    Sep 7, 2008
  4. Stefan Mueller
    Replies:
    3
    Views:
    203
    Stefan Mueller
    Mar 21, 2006
  5. Paul
    Replies:
    1
    Views:
    1,076
Loading...

Share This Page