I'm sending some html inserted in an (with CKEditor) to a .php trough jQuery ajax() and have that PHP return the html formatted and put it back in the textarea.
This is the Ajax call
var elhtml = document.getElementById('products_description[1]');
$.ajax({
type: "POST",
dataType: "json",
data: 'elhtml=' + elhtml.value,
url: 'categories_cleanup.php',
success: function(data) {
$('#textarea').val(data.message);
}
});
And this is the PHP
<?php
echo json_encode(array("message"=>strip_tags( $_POST['elhtml'], '<ul><li>')));
?>
The problem is that when the html comes back it gets trimmed. So I start debugging and found that it always gets trimmed at the point where there's a special character. If I place the same HTML that the ajax sends to the PHP where the $_POST var is, the HTML gets back to the textarea just fine.
So the problem must be the trip from the textarea to the php. The code get's trimmed anytime there's an ' , for example: It's -> It's The code form the PHP will come back from zero to the first appeareance of ' [...] It
Is this an ajax encoding issue? I tried setting the ajax dataType to text but the same happens.
I don't think that has anything to do with PHP since it can handle it perfectly when the html was inserted in the php itself.
Any ideas? Thanks in advance
The curly quote isn't in ISO 8859, the default encoding. Try forcing PHP to send the header Content-Type: application/json; charset=utf-8
.
You can check the headers used in AJAX transactions in the Net tab in Firebug (jQuery's ordinary AJAX requests also show up in the console).
The answer was provided by ThiefMaster:
Replace 'elhtml=' + elhtml.value with {elhtml: elhtml.value} - otherwise you have to take care of escaping etc