0

I have a text form with CKEditor:

<textarea id="descrizione_it"></textarea>
<textarea id="descrizione_fr"></textarea>
... many others inputs ...    
<button onclick="addDataProd()">

The JS Function:

function addDataProd(){
    var descrizione_it = CKEDITOR.instances.descrizione_it.getData()
    var descrizione_fr = CKEDITOR.instances.descrizione_fr.getData()

    if (window.XMLHttpRequest) {
      xmlhttp=new XMLHttpRequest();     
    } else {
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        alert(xmlhttp.responseText);
      }
    }

xmlhttp.open("GET","prodotti_ajax.php?descrizione_it_new="+descrizione_it + "&descrizione_fr_new="+descrizione_fr,true);
xmlhttp.send();
}

And the PHP with get the data and updates the database.

Now if I wrote some special charaters on the CKEditor textarea, those charaters are always and immediatly converted tu htmlentities (è).

If I write on the JS

alert(descrizione_fr)

I get the string as written in CKEditor e.g.

èèèà

but when I echo the string in php the string stops just before the special charaters.

E.g. JS:

alert(descrizione_fr)

results in:

test_test_&egrave;test

PHP:

echo $_GET['descrizione_fr_new'];

results in:

test_test_

How can I solve that?

Thank you!

user2120569
  • 227
  • 1
  • 3
  • 9

1 Answers1

0

You can try this function that use the browser parser to do it. DEMO

function html_decode(text){
    var div = document.createElement("div");
    div.innerHTML = text;
    return ("textContent" in div) ? div.textContent : div.innerText ;
}

I don't know if it's the best way. But I found this solution in jQuery and made it pure javascript. Hope it help

EDIT: using textContent(IE9+) as default and innerText as fallback for compatibility

Community
  • 1
  • 1
fernandosavio
  • 9,849
  • 4
  • 24
  • 34
  • Thank you. With encodeURIComponent(var_to_encode) I get the same results... Maybe it's better to use this? – user2120569 Apr 09 '13 at 21:16
  • I'm thinking about `innerText` property.. I don't know if Firefox recognize it.. but there's `textContent` too.. – fernandosavio Apr 09 '13 at 21:19
  • This just solve your html encode/decode problem.. Use it on your `alert(descrizione_fr)` like `alert(html_decode(descrizione_fr))`.. Use it only if you know the content, it will parse script tags as well.. – fernandosavio Apr 09 '13 at 21:24