-1

I am traying to send special characters to a php server. I tried with angular and with jquery getting the same result:

myClient.js with JQuery:

data = "Ñoñón";
dataToSend={special_word: data};
$.ajax({    
  type : 'POST',
  url  : 'myServer.php',
  data : dataToSend,
  success :  function(response){
    console.log(response);
 }
});

myServer.php for JQuery client:

$special_word =mysqli_real_escape_string($mysqli, $_POST["special_word"]));
saveWordInDB($special_word);
echo $special_word;

myClient.js with angular:

data = "Ñoñón";
dataToSend={special_word: data};
$http.post('myServer.php', dataToSend)
.then(
   function(response){
     // success callback
     console.log(response.data);
   }, 
   function(response){
     // failure callback
     console.log(response);
   }
);

myServer.php for angular client

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$special_word = $request->special_word;
saveWordInDB($special_word);
echo $special_word;

In both cases, the word "Ñoñón" is not stored in the DB, but the word "ñoñón" its stored instead. The same occurs with the word showed in the console. How can this be fixed?

Thanks in advance!

  • Do you set the encoding of your DB connection to UTF8? Is your database set to utf8? Does `$postdata` have the correct value, does `$request`, does `saveWordInDB` return it correctly? Where do you run the insert? – user3783243 Aug 14 '18 at 17:29
  • Yes to all. In fact, I have more special characters saved in the DB. When I send a special character directly from a
    – Sergio Ferrer Sánchez Aug 14 '18 at 17:32
  • If `$postdata` outputs as the correct character than the JS sounds like it was implemented correctly, no? – user3783243 Aug 14 '18 at 17:39
  • That's it. I'm pretty sure it's not an implementation error :S – Sergio Ferrer Sánchez Aug 14 '18 at 17:44
  • 1
    If `$postdata` gets it than it is a PHP issue. If not than it is an issue with transmitting the request. You need to narrow down where the issue is. Use the developer console, and the PHP error log with `error_log` to see where the value is lost. – user3783243 Aug 14 '18 at 17:49

1 Answers1

1

I think you're having utf8_decode issue here, try to decode it before saving in DB

$special_word 
=mysqli_real_escape_string($mysqli, 
$_POST["special_word"]));
saveWordInDB(utf8_decode($special_word));
echo $special_word;

Ref. http://php.net/manual/en/function.utf8-decode.php

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103