title: Character encoding problem in PHP/MySQL/jQuery
body: <p>I made some CMS in PHP which manipulates with data from MySQL.<br>
  In my CMS, i have some input fields in which I would like to have jQuery's fancy autocomplete implemented. Basically, the idea is to create jQuery's arrays from MySQL tables...</p>
  
  <p>I'm working with PHP 5.3.0, MySQL 5.0.82 and Eclipse 3.4.2. My PHP project in Eclipse is UTF-8 encoded. My CMS pages are in UTF-8 character encoding (<code>&lt;meta http-equiv="Content-type" content="text/html; charset=UTF-8" /&gt;</code>). Website itself is also in UTF-8 encoding. MySQL database, and all tables are in UTF-8 (utf8_general_ci).</p>
  
  <p>At this point everything works fine if I am entering only letters (even international and some weird symbols), but if I enter some of these characters <code>"</code>, <code>&amp;</code>, <code>&lt;</code> or <code>&gt;</code>, I run into some serious problems...</p>
  
  <p>When I enter such text, everything looks fine in my database, but when I try to create an array for jQuery, it creates an error because of quotes (I suppose that even single quotes are problem here)... So I suppose I should escape them somehow, right? Then I use PHP's <code>htmlspecialchars</code> and jQuery's array is created correctly. Even when I click inside input field and start typing text, autocomplete shows all those characters correctly. But if I actually select one of the records with those characters, they suddenly appear like html's escaped characters (<code>&amp;quot;</code>, <code>&amp;amp;</code>, <code>&amp;lt;</code>, <code>&amp;gt;</code>). So I tried to apply <code>htmlspecialchars_decode</code> to that same input field but it didn't help... Is there a way to display those characters correctly in my input field when I select a record from jQuery's autocomplete?</p>
  
  <p>I've tried to google the problem, but I couldn't find anything to solve my problem... Please help!<br>
  Thanks in advance!</p>
  
  <hr>
  
  <p>EDIT:
  This is the way I am creating an array for jQuery (<code>$tags</code> is just a simple array):</p>
  
  <pre><code>&lt;?php
  $t = implode(", ", $tags);
  ?&gt;
  &lt;script&gt;
  $(document).ready(function(){
      var data_tags = "&lt;?php echo htmlspecialchars($t); ?&gt;".split(" | ");
      $("#input_tags").autocomplete(data_tags, { multiple: true, multipleSeparator: ", " });
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>I know it is maybe not the best way, but generally it works :)</p>
  
  <p>I am generating an input field this way:</p>
  
  <pre><code>&lt;?php
  
  function inputField($label, $name, $type, $size, $default=NULL, $misc=NULL){
  
      $printInput = "&lt;tr&gt;&lt;td align=\"right\" valign=\"top\"&gt;\n";
      $printInput .= $label;
      $printInput .= "&lt;/td&gt;&lt;td&gt;\n";
      $printInput .= "&lt;input type=\"".$type."\" size=\"".$size."\" name=\"".$name."\" id=\"".$name."\" value=\"".$default."\"&gt; ".$misc."\n";
      $printInput .= "&lt;/td&gt;&lt;/tr&gt;\n";
  
      return $printInput;
  
  }
  
  echo inputField("TAGS", "input_tags", "text", 70, $db_tags);
  ?&gt;
  </code></pre>