Solved

HtmlEncode and Curly Quotes, from Mysql to Ajax to Textarea, back to Mysql

Posted on 2012-04-03
10
353 Views
Last Modified: 2012-12-09
I need help on properly ENCODING the following:

1 - grab a record in MySQL with French Characters and curly braces
2 - pass it via ajax to a textarea
3 - view all foreign characters normally inside textarea
4 - edit text and send it back for update via ajax to MySQL

Can you provide a simple example on how to grab this text, edit it, and update it with proper encoding.

Je m’apelle François, J’ai “tois enfants”
Gérard et à “wow” c’est bon àâçéèêëïîôùù

This may be simple to a seasoned programmer, but it's been kicking  my you know what...

I tried htmlentities() before sending to ajax but that didn't work, help.
0
Comment
Question by:dimsouple
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 7

Accepted Solution

by:
designatedinitializer earned 500 total points
ID: 37804118
The fundamental thing to have in mind is to use UTF-8 encoding. Use UTF-8 in your database and in your php files.

In your ajax, use some serialization function.
I assume you are using jQuery. (if you're not, then you should).
If so, here's an example of an ajax request:

		$.ajax({
			type	: "POST",
			cache	: false,
			url		: "../participar.php",
			data	: $("#prizeForm").serializeArray(),
			success	: function(data) {
			parent.$.fancybox(data);
			parent.$.updateAddress();
			}

Open in new window


And here is a snippet of the PHP code that receives the ajax request.
You simply treat it as POST:
if(array_key_exists('action', $_POST)) {	switch($_POST['action'])
	{
		case "alterar":
			// This is an AJAX request from the main window
			$user = new user();
			$user = $session->getVar("user");
			if(!is_a($user,"user")) {
				// Logout
				$session->delete("user");
				die("A sua sessão terminou. Por favor faça login novamente.");
			}
			$name     = trim($_POST['altNome']);
			$password = trim($_POST['altPass']);

...

Open in new window

Then be careful to use mysql_real_escape_string() on all user input, before inserting into the database.
0
 
LVL 7

Assisted Solution

by:designatedinitializer
designatedinitializer earned 500 total points
ID: 37804126
IMPORTANT: in your text editor, be sure to change your php files' encoding to "UTF-8, with no BOM"!
(Simply put, the BOM is a bunch of non-visible garbage that gets into the start of your file and can mess with your request headers and spawn misterious errors)
0
 
LVL 82

Expert Comment

by:leakim971
ID: 37804215
... and if you already have records in an other encoding(latin-1/ISO 8859-1), you should consider this data as corrupted
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37810390
You do not need unicode for western european characters.  ISO-8859-1 works perfectly.  The central issue with this or any other encoding problem is getting consistency across the platforms.  This article explains some of it.
http://www.joelonsoftware.com/articles/Unicode.html

See http://www.laprbass.com/RAY_temp_dimsouple.php
<?php // RAY_temp_dimsouple.php
error_reporting(E_ALL);

$html = <<<HTML
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="iso-8859-1" />
<title>Accented Characters in ISO-8859-1</title>
</head>
<body>
<p>
Je m’apelle François, J’ai "tois enfants"
Gérard et à "wow" c’est bon àâçéèêëïîôùù
</p>
</body>
</html>
HTML;

echo $html;

Open in new window

0
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37810497
@Ray: Of course ISO-8859-1 encodes french diacritics and such, but there are strong reasons for ditching it in favor of utf-8 (as Joel does in the article you posted...)
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37810607
The one reason I would be careful about ditching any ANSI font goes to the need for consistency across all the levels of the platform.  This means the data base, the file system, things that were stored in cookies, client keyboard input, JavaScript, values created inside scripts, HTML, etc.  Any of these things may come with the legacy assumption that they are all single-byte characters.  That assumption may lead to encoding collisions, and in my experience the resulting encoding collisions are very difficult to explain since the conversion to UTF-8 may be difficult for financial managers to understand.  A common response goes something like, "You did what?  It was working before.  Why did you eff with it?"
0
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37810629
I do agree with you on this: if it is working, there's no need to fix it.
However, if you are starting something from scratch, always go with Unicode.
0
 

Author Comment

by:dimsouple
ID: 37816468
Thank you all so much. the part about the data being corrupted is no lie. because I failed to specify the charset in the old pages, the form input were coming in in many different formats.

now I've changed everything to UTF-8 and unfortunately, some of the data is in other format.

I've found out that this does the trick on the coruppted data

$thisARY['message']=iconv("Windows-1252","UTF-8",$thisARY['message']);
0
 
LVL 7

Assisted Solution

by:designatedinitializer
designatedinitializer earned 500 total points
ID: 37817122
People like me are eagerly (not that much, but anyway...) awating for the next major release of PHP, which supposedly is going to have native support for Unicode, down to variable names and other language tokens.
Meanwhile, we use UTF-8 and we are careful to specify utf-8 files with no BOM.

Other useful features are the utf8_encode and utf8_decode PHP functions, and in MySQL, the ability to specify the character encoding down to the SELECT level: you can have different SELECT statements specify different character encodings.
One other thing to keep in mind is that the character encoding is not the collation (some people tend to confuse these two).
0
 
LVL 82

Expert Comment

by:leakim971
ID: 37833084
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question