Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

jQuery charset problem WordPress

  • SOLVED

I'm tring to echo the page title with jQuery but get a charset encodinng problem for '

Ex: L'assomption commes out like this COLLÈGE L’ASSOMPTION

but all other special chars come out ok éèàû....

And i tried a lot of different things nothing works!!! HELP!!



<?php
header('Content-Type: text/html; charset=utf-8');
$theSchoolTitle = get_the_title();
echo
'
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$(function () {

$.ajaxSetup ({
"beforeSend" : function(xhr) {
xhr.overrideMimeType("text/html; charset=utf-8");
}
});

$("#schoolName").attr("value", "'.$theSchoolTitle.'");
$("#schoolName").attr("readonly", true);

});
});

</script>

Answers (3)

2011-05-24

Denzel Chia answers:

Hi,

Have you tried using PHP function html_entity_decode() ?
http://php.net/manual/en/function.html-entity-decode.php

Try adding the following before your jQuery.


$theSchoolTitle = get_the_title();
$theSchoolTitle = html_entity_decode($theSchoolTitle);


Thanks.
Denzel


Roberto Mas comments:

Hey Denzel,

Yes i tried that also but it didint work. Actualy if i echo the php var it come out fine the problem appears when the var is passed to the javascript.

Hope this can help


Denzel Chia comments:

Hi,

Have you tried without any charset encoding?
I always use jQuery Ajax without any charset encoding and it never encodes my data sent.
I only use url_encode and url_decode on the receiving PHP script.

Thanks.
Denzel


Roberto Mas comments:

Yes :( My original script had no encoding. Since that did not work i tried the encoding on php first than realised that the php was fine and the problem realy comes from the js so i tried to use encoding on the js and it changed nothing. I even tried with ajax and still not working :(

$.ajaxSetup({
'beforeSend' : function(xhr) {
xhr.overrideMimeType('text/html; charset=UTF-8');
},
});


Denzel Chia comments:

Hi,

I always use jQuery ajax like this, and wrapped with document.ready and triggered by a function.


$.ajax({
type: "POST",
url: "http://example.com/some.php",
data: "school_name=school_name_value",
success: function(msg){
alert( "Data Saved: " + msg );
}
});


and never with


$.ajaxSetup({
'beforeSend' : function(xhr) {
xhr.overrideMimeType('text/html; charset=UTF-8');
},
});


Perhaps, sending the school title using the above ajax code to a php processing script will work?
See what the processing script receives first by print_r() the $_POST['school_name'] and see if html_entity_decode works on the $_POST value.

Sorry, this is all I can think of.
Perhaps there is a jQuery decode function to use, which I did not know.

Thanks.
Denzel

2011-05-25

Jimish Gamit answers:

create a SPAN or P tag and assign id '#schoolName_temp' to it and change in code as below
it will work for u :)
<?php
header('Content-Type: text/html; charset=utf-8');
$theSchoolTitle = get_the_title();
echo
'<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$(function () {
$.ajaxSetup ({
"beforeSend" : function(xhr) {
xhr.overrideMimeType("text/html; charset=utf-8");
}
});
$("#schoolName_temp").html( "'.$theSchoolTitle.'");
$("#schoolName").attr("value", $("#schoolName_temp").html());
$("#schoolName").attr("readonly", true);
});
});
</script>';


Jimish Gamit comments:

hide that SPAN / P tag


Roberto Mas comments:

smart :). This worked. So what it'S because i was outputing my var directly into an input field?

2011-05-24

David Navarrete answers:

try to save the file with the same encode with the wordpress.


Roberto Mas comments:

Did that alrteady. The file , the document the html page charset the php header and the javascript all have the utf-8 encoding.