0

I have a problem with encoding/decoding some text.

On my page I have a picture:

 <img id ="pcs" src="xxx.jpg" game="Hellblade: Senua&#x2019;s Sacrifice ">

Then use ajax to send data to php server:

$('#pcs').click(function(){
 if($(this).attr('src') == "xxx.jpg" ) {
  $(this).fadeOut(100, function() {
   var cookie = "dummy";
   var game = $(this).attr('game');
   $.ajax({
    url: "http://example/notif_games.php",
    type: "POST",
    data: ({'cook_':'cookie', 'game_': game}),
  });

The problem is the data are somehow encoded and recieved as: Hellblade: Senua’s Sacrifice

Instead, I need it to be recieved as: Hellblade: Senua&#x2019;s Sacrifice

Even where I try

var game = $(this).attr('game');
alert(game);

it returns the 'encoded' value.
It's needed with the &#x2019; part because I then put it in DB and compare it with another tables where the value is with the &#x2019;.

not_loqan
  • 91
  • 1
  • 1
  • 9
  • Try use this: `var game = $('#pcs').html($(this).attr('game')).text();` – MG_Bautista Apr 03 '18 at 20:52
  • See answer here: https://stackoverflow.com/questions/1219860/html-encoding-lost-when-attribute-read-from-input-field – Daniel Apr 03 '18 at 21:01
  • 1
    Possible duplicate of [HTML-encoding lost when attribute read from input field](https://stackoverflow.com/questions/1219860/html-encoding-lost-when-attribute-read-from-input-field) – Daniel Apr 03 '18 at 21:02
  • Well, thanks guys but none of those answers seems to work: >! `function htmlEncode(value){ // Create a in-memory div, set its inner text (which jQuery automatically encodes) // Then grab the encoded contents back out. The div never exists on the page. return $('
    ').text(value).html(); } function htmlDecode(value){ return $('
    ').html(value).text(); } alert (htmlDecode($('#pcs').attr('game')));`
    – not_loqan Apr 03 '18 at 21:22

1 Answers1

0

This is a browser issue and not a jquery one. Your browser is rendering the text already before jquery touches it. You need to decode it on the server side when you ajax it up.

With php you can do $data = htmlentities($data);

Cesar Bielich
  • 4,754
  • 9
  • 39
  • 81
  • Thanks for answer. I have tried this but it returns `Hellblade: Senua’s Sacrifice `, I need it's hex value as written here [link](http://www.codetable.net/hex/2019) – not_loqan Apr 03 '18 at 21:33