Character Encoding Problem in PHP / MySQL / jQuery

I made several CMS in PHP that manipulate data from MySQL.
In my CMS, I have some input fields in which I would like to have jQuery fancy autocomplete. Basically, the idea is to create jQuery arrays from MySQL tables ...

I work with PHP 5.3.0, MySQL 5.0.82 and Eclipse 3.4.2. My PHP project in Eclipse is encoded in UTF-8. My CMS pages are in UTF-8 character encoding ( <meta http-equiv="Content-type" content="text/html; charset=UTF-8" />). The site itself is also used in UTF-8 encoding. The MySQL database and all tables are in UTF-8 (utf8_general_ci).

At this stage, everything works fine, if I enter only letters (even international, and some strange characters), but if I enter some of these symbols ", &, <or >I run into some serious problems ...

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 due to quotes (I suppose that even single quotes are problems here) ... So, I suppose I must somehow escape from them, right? Then I use PHP htmlspecialcharsand the jQuery array is created correctly. Even when I click inside the input field and start typing, autocomplete displays all these characters correctly. But if I actually select one of the entries with these characters, they suddenly appear as html escaped characters ( &quot;, &amp;, &lt;, &gt;). So I tried to apply htmlspecialchars_decodeto the same input field, but that didn’t help ... Is there a way to correctly display these characters in my input field when I select an entry from jQuery autocomplete?

, , ... , !
!


EDIT: jQuery ($tags - ):

<?php
$t = implode(", ", $tags);
?>
<script>
$(document).ready(function(){
    var data_tags = "<?php echo htmlspecialchars($t); ?>".split(" | ");
    $("#input_tags").autocomplete(data_tags, { multiple: true, multipleSeparator: ", " });
});
</script>

, , :)

:

<?php

function inputField($label, $name, $type, $size, $default=NULL, $misc=NULL){

    $printInput = "<tr><td align=\"right\" valign=\"top\">\n";
    $printInput .= $label;
    $printInput .= "</td><td>\n";
    $printInput .= "<input type=\"".$type."\" size=\"".$size."\" name=\"".$name."\" id=\"".$name."\" value=\"".$default."\"> ".$misc."\n";
    $printInput .= "</td></tr>\n";

    return $printInput;

}

echo inputField("TAGS", "input_tags", "text", 70, $db_tags);
?>
+3
2

json_encode(), PHP 5.2.0 .

EDIT json_encoded:

var data_tags = <?php echo json_encode($t); ?>;
+4

htmlentities();.

0

Source: https://habr.com/ru/post/1730797/


All Articles