0

We have some ASCII and Unicode characters which we want users to use when they are imputing text into a Form’s text and textarea fields.

For example,

$SpecialCharacters = array(
    'single quotes'=>''',
    'double quotes'=>'"',
    'copyright'=>'©',
    'registered'=>'®',
    'trademark'=>'™',
);

We are using jQuery UI Drag and Drop to move the selected non-standard character from the toolbar to the input field. That works fine.

However, when the data is saved to the MySQL database (using PHP) and returned, the non-standard characters return in a malformed encoding.

  • User inputs: bill.onthebeach™
  • Returned page displays: bill.onthebeachâ¢

Everything is UTF-8 encoded.

So I'm thinking this should solve the problem:

  • User inputs: bill.onthebeach™
  • Returned page displays: bill.onthebeach™

The question is, Is there a jQuery function or plug-in that will convert non-standard characters to their encoded forms?

 ™ => ™

Or, Does the problem lie elsewhere? If so, what’s the solution?

TRiG
  • 10,148
  • 7
  • 57
  • 107
Bill Warren
  • 392
  • 8
  • 11

3 Answers3

1

The question is, Is there a jQuery function or plug-in that will convert non-standard characters to their encoded forms?

You say “non-standard characters” but you actually mean “HTML character references”. Anyway, you can achieve what you want by using the he JavaScript library:

he.encode('bill.onthebeach™');
// → 'bill.onthebeach™'

he.decode('bill.onthebeach™');
// → 'bill.onthebeach™'

he.decode('bill.onthebeach™');
// → 'bill.onthebeach™'

However, it seems the root of the problem lies elsewhere – there’s no point in using he at runtime.

Mathias Bynens
  • 144,855
  • 52
  • 216
  • 248
0

As far as I know, Javascript is always UTF-8. And this is a standard you can’t change. I wasted a lot of time using PHP to change the encoding of strings for older pages I work on, then I found out that you just need to set your MySQL connection to utf8 and your HTML page to <meta charset="UTF-8">.

As long as you have those two settings set and your characters look fine in your HTML, you should be fine, no matter what.

I guess that it’s either your MySQL connection or your HTML page that is missing the utf8 setting. Also, if you have these settings it doesn’t matter at all which collation your database fields use.

TRiG
  • 10,148
  • 7
  • 57
  • 107
low_rents
  • 4,481
  • 3
  • 27
  • 55
  • thanks.. our settings look like this... on the page... ... and in DB (PhpMyAdmin ) - utf8_unicode_ci... which does not fixed the issue illustrated above.... – Bill Warren Nov 15 '14 at 21:24
  • 1
    @BillWarren. Having the page and the database in UTF-8 is no good if the database *connection* is not also in UTF-8. See "[UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-throughhttp://stackoverflow.com/questions/279170/utf-8-all-the-way-through)". – TRiG Nov 19 '14 at 14:41
  • @TRiG yes, the database collation does not even matter. if you got a `latin_general_ci` collation somewhere in your database then `MySQL` will do all the work for you, als long as your connection is set to `UTF-8`. – low_rents Nov 20 '14 at 09:51
0

Thanks guys...

Here's the basics of what works... the long version is too specific to our purposes to be useful..

  1. TRiG and others are right... UTF-8 'all the way'... means 'all the way'... DB and tables are set to - UTF8_unicode_ci

  2. However, if you do that and this in your php --

    $data = utf8_encode($data);

The special characters are going to display "garbled"... so don't do both...

  1. The jQuery draggable / droppable Object 'ui'.... does give us options to do things with special characters on the page, like search and replace ... once you've located this...

    var char = ui['helper']['prevObject'][0]['firstChild']['data']

After that it's all full-speed straight ahead...

We've got it working smooth for html, ascii and unicode special characters.

Bill Warren
  • 392
  • 8
  • 11