2

I'm struggling getting a pound sign (£) back from my Ajax call.

This is the jQuery call I have

(function(d){
   var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
   js = d.createElement('script'); js.id = id; js.async = true;
   js.src = "//connect.facebook.net/en_GB/all.js";
   d.getElementsByTagName('head')[0].appendChild(js);
}(document));

$.post('ajax.php?vno=' + new Date().getTime(), {
    action: 'submit'
}, function(res){
    if (res.status) {
        alert(res.post);
        FB.api('/me/feed', 'post', { message: res.post },function(){});
    }
},'json');

In my PHP script, I pass back html_entity_decode("£"); but the alert shows £.

If I change my PHP code to html_entity_decode("£"); the alert box shows nothing

How can I get the alert to show £?

Damian
  • 1,652
  • 4
  • 26
  • 44

1 Answers1

3

Javascript alerts don't interpret HTML entities (see the answer to that question for an example of how to decode the entity before alerting it.) If you're just testing with the alert something that you're actually going to insert into the HTML web page, then if you see £, then your final code should work fine.

But there's no great need to use an entity, anyway. If the character encoding for your Ajax return and your web page are set right (e.g. both UTF-8), then just returning '£' should work in the alert and the page. Trust me, I'm British. We have a £ key on our keyboards and it pretty much just works as a character on the modern web :)

Community
  • 1
  • 1
Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
  • Thanks Matt, I should've mentioned I'm posting the return to a facebook user's wall - FB.api('/me/feed', 'post', { message: res.post },function(){}); – Damian Jan 12 '14 at 17:12
  • In that case, I'd just use a standard UTF-8 £ symbol. Facebook will be fine with it. Make sure everything you have is declared as using UTF-8. – Matt Gibson Jan 12 '14 at 17:13
  • When I try passing £ back, nothing is posted to the wall. If I pass back £, it posts £ – Damian Jan 12 '14 at 17:15
  • You may want to update your question to show the complete code that's actually causing your problem, as well as making it clear which Facebook SDK, etc. you're using. – Matt Gibson Jan 12 '14 at 17:17
  • @Damian (Also: is your web page definitely UTF-8? i.e. does it have a `` header, or equivalent? And does your PHP script send a Content-Type header to match, e.g. `header('Content-Type: text/html; charset=utf-8');`?) And are both your PHP file and your HTML file saved in UTF-8 encoding? Everything should be UTF-8 from end to end. – Matt Gibson Jan 12 '14 at 17:20
  • meta tag is utf-8. I'm not sure what you mean by the php file sending back a content-type header to match, it just returns a json encoded array – Damian Jan 12 '14 at 17:34
  • When I use $arr['post'] = "£"; to return the £ sign, looking at the console, shows "post":null so it's definitely not passing back the £ sign – Damian Jan 12 '14 at 17:36