SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast wallyweb's Avatar
    Join Date
    Oct 2003
    Location
    Saint John, NB, Canada
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Safari. Ajax. textarea and accents issue

    I have a form in which one of the fields is a textarea.
    The contents of the form are submitted to a MySql database via Ajax with http.open("POST", url, true);.
    The header is set with http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    Character encoding is UTF8.

    The contents are then selectively (on user input) rendered on a web page via Ajax.
    All works great on Windows and on OS/X using Firefox.
    Unfortunately there is an issue with Safari.
    It seems that letters with accents such as , , . and ... etc. do not render properly.
    Safari will render something like É instead of .

    Any ideas as to why this happens and how it might be resolved?

  2. #2
    SitePoint Zealot trekmp's Avatar
    Join Date
    Dec 2005
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I read somewhere that Safari uses iso-8859-1 character set, try setting this to use UTF8 and see what happens. Safari Encodes the querystring, you may need to decode this

    Hope this helps
    Why do we exist? What is our purpose?
    >H2O Developments
    >USS Endeavour
    >Hosted@Servage

  3. #3
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried setting accept-charset="utf-8" on the form?
    Birnam wood is come to Dunsinane

  4. #4
    SitePoint Enthusiast wallyweb's Avatar
    Join Date
    Oct 2003
    Location
    Saint John, NB, Canada
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by trekmp
    I read somewhere that Safari uses iso-8859-1 character set, try setting this to use UTF8 and see what happens. Safari Encodes the querystring, you may need to decode this

    Hope this helps
    Actually the default setting was Latin-1 and we tried UTF-8 with no success.

    Quote Originally Posted by AutisticCuckoo
    Have you tried setting accept-charset="utf-8" on the form?
    This got me thinking (oh the pain, the pain) and I Googled form accept-charset="utf-8"
    The first response was this site from IBM which has an excellent discussion that seems to address my issue. I am studying it now and will post my results back here.

    Thank you folks for your quick responses.

  5. #5
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've never had a problem with Safari submitting or reading UTF-8 data. As long as both the browser and MySQL knows it is UTF-8 data, and as long as when you enter it into MySQL you tell the MySQL client it is UTF-8, there should not be a problem.

  6. #6
    SitePoint Enthusiast wallyweb's Avatar
    Join Date
    Oct 2003
    Location
    Saint John, NB, Canada
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dreamscape
    I've never had a problem with Safari submitting or reading UTF-8 data. As long as both the browser and MySQL knows it is UTF-8 data, and as long as when you enter it into MySQL you tell the MySQL client it is UTF-8, there should not be a problem.
    Firebird on the Mac in question takes the accented letters submitted in the textarea with utf-8 and returns the characters properly but Safari on the same Mac does not.
    Is Firebird more foregiving than Safari in this regard?
    All settings and encodings are utf-8. I'm sure the MySql server supports utf-8 (I don't have configuration privileges to it) because it does support a forum that is utf-8.
    Somewhere I read that the encoding for textareas is set by the o/s as opposed to the browser but that still does not expain how Firebird gets it right and Safari doesn't on the same machine. If I knew what the difference was I could work it out.

  7. #7
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wallyweb
    All settings and encodings are utf-8.
    Well I beg to differ. I work with Safari & UTF-8 pretty much every day, and the only time I've ever seen it render supposed UTF-8 data improperly is because something in the chain does not know or tell the others that it is UTF-8.

    Some things to check:
    • Check that the HTML page which has the form is declared as UTF-8 data or sending a UTF-8 header. Ideally you should both be sending a UTF-8 header in addition to the explicit declaration in the HTML document.
    • Check that the MySQL database table column that the data is entered into has a UTF-8 Collation (typically utf8_general_ci). For MySQL 4.1+
    • Check that when you send or receive data to/from MySQL to whatever server side language you are using, that you tell MySQL that the client character set is UTF-8, that you want the results in UTF-8, and that the connection character set is UTF-8. The easiest way to take care of all 3 being UTF-8 is to issue the query SET NAMES utf8 after you connect to the database. For MySQL 4.1+
    • Check that the server side language knows the data is in UTF-8 format if necessary. (e.g: For PHP if you use MB String functions on the data, make sure you set the MB String encoding settings to UTF-8. If you use any PCRE functions on the data, make sure to use the Unicode modifier where appropriate. And in PHP it doesn't hurt to set the INI default_charset setting to UTF-8 either [you can use ini_set() on this setting])
    • Check that when the data is sent back to the AJAX client, you tell the client that it is UTF-8. [e.g header('Content-Type: text/xml; charset=utf-8') in PHP]


    Don't assume that a client along the way will automatically pick up that the data is UTF-8. Always tell the next client you're communicating with in the chain that the data is UTF-8, from the browser to the database and back again.

  8. #8
    SitePoint Enthusiast wallyweb's Avatar
    Join Date
    Oct 2003
    Location
    Saint John, NB, Canada
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dreamscape
    Well I beg to differ. I work with Safari & UTF-8 pretty much every day, and the only time I've ever seen it render supposed UTF-8 data improperly is because something in the chain does not know or tell the others that it is UTF-8.
    Sorry if I was a bit abrupt. What I meant was that the web pages and the browsers were all set to UTF-8.
    The point about something in the chain was exactly what I suspected and was looking for, but not being a Mac user myself I couldn't be sure.
    Your "things to check" list is an ideal roadmap and I will be reviewing it closely. I've already noticed a couple of items that I need to fix.
    By the way, and I should have mentioned this before, the server side language is PHP using the mysqli functions.
    Thanks for this. It is greatly appreciated.

    I will post my progress here for all to share.

  9. #9
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suspect your problem lies with the last checkpoint about telling the AJAX client that the XML response is UTF-8. I think most browsers assume that the data in an XML HTTP Request's response is UTF-8, but Safari does not.

    The database could also be a problem in the future if you want to do stuff with the data but are not storing it as UTF-8. If you're just passing it in and out of the database for the moment, then for the most part you probably won't notice that it is not stored in UTF-8, but that may come back to bite you when you want to start doing stuff with the data, so I find it is always best to store data in its intended format.

    Anyways, let us know how things go for you

  10. #10
    SitePoint Enthusiast wallyweb's Avatar
    Join Date
    Oct 2003
    Location
    Saint John, NB, Canada
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dreamscape
    Anyways, let us know how things go for you
    Step 1: Read the manual
    http://dev.mysql.com/doc/refman/4.1/en/charset.html
    Stay tuned

  11. #11
    SitePoint Enthusiast wallyweb's Avatar
    Join Date
    Oct 2003
    Location
    Saint John, NB, Canada
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Quote Originally Posted by wallyweb
    Step 1: Read the manual
    http://dev.mysql.com/doc/refman/4.1/en/charset.html
    Stay tuned
    Step 2:
    Troubleshoot!
    I found that although just about everything was set for utf-8, ajax was loading latin-1 into the pages.
    This was strange because PHP is supposed to default to utf-8 and all the pages are .php.
    The errant text had to have been loaded into the MySql database as latin-1 or the database was defaulting to latin-1.
    As control over the MySql configuration was with my host I could not change it.
    Step 3: Read another manual
    The one on PHP - here specifically
    Step 4: Try it out
    Here's the code I added right after connecting to the database:
    Code:
    if (!mysqli_set_charset($dbcnx, "utf8")) {
    	printf("Error loading character set utf8: %s\n", mysqli_error($dbcnx));
    }
    Step 5: Test it!
    It worked!
    Thank you one and all.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •