0

Using AJAX, jQuery and JSON, I'm adding a feature to a CMS panel allowing the user to select a page to edit from a dropdown menu, and then populating an input field and a textarea with the corresponding data from the db.

This already works fine, but for some reason the information is only being properly inserted into the fields if no special characters beyond the english alphabet are entered. I tried with ÆØÅ for instance, and these are a must-have for the product to be of any use.

Is there something inte the json_encode or jQuery.parseJSON() functions that stop me from using the Danish letters?

$.ajax({
    type: 'post',
    url: 'cms.php',
    dataType: 'json',
    data: { page: value },
    success: function(data) {
        var json = jQuery.parseJSON(data);
        $('#title').val(json.title);
        $('#pg').val(json.pg);
    }

});
Mikkel Winther
  • 567
  • 1
  • 4
  • 10
  • 2
    JSON data must be UTF-8 encoded. – Felix Kling Jun 13 '13 at 12:35
  • And UTF-8 supports æøå, so no, there shouldn't be anything stopping you from using scandinavian letters. – adeneo Jun 13 '13 at 12:39
  • And setting the dataType to JSON automatically parses the result, so now you're doing it twice ? – adeneo Jun 13 '13 at 12:41
  • So where would the problem reside, if there is no issue here? – Mikkel Winther Jun 13 '13 at 12:44
  • And I parse the result a 2nd time, because I couldn't figure any other way to extract the .title and .pg elements. Data.title and Data.pg does not work. – Mikkel Winther Jun 13 '13 at 12:44
  • 1
    There are too many unknowns here. What does "not properly" mean exactly? What exactly are you doing server side? What encoding are you outputting? If you can't answer that, start here: [What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text](http://kunststube.net/encoding/), [Handling Unicode Front To Back In A Web App](http://kunststube.net/frontback/), [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – deceze Jun 13 '13 at 12:47
  • Not properly means that "abc" gets inserted, and "abcø" does not. No text is being input at all. So the server side should be fine, in my opinion. – Mikkel Winther Jun 13 '13 at 12:56
  • Does the server get any data at all? If so, what? What does the data sent to the server look like? Use the network inspector of your browser to debug. Does it fail at the Javascript level or at the database level? **Debug** in as much detail as possible! – deceze Jun 13 '13 at 13:06
  • Looking at the network inspector in Google Chrome I only see that the cms.php script is being queried. I dont see any failures anywhere, and that would surprise me too, seeing as how some data gets passed through fine, but other data is being stopped when it isn't only english letters. – Mikkel Winther Jun 13 '13 at 13:22
  • $mysqli->set_charset('utf8'); did the trick for me. Thank you for the links! – Mikkel Winther Jun 13 '13 at 15:24

0 Answers0