I have jQuery autocomplete setup on a text input and apostrophes are not being converted from ' which is how they are stored in the database.
Autocomplete drop-down displays Bill's Apartments instead of Bill's Apartments.
From my understanding the browser converts ' to and apostrophe, '.
Is the data being double encoded? Is jQuery encoding the data it is receiving via $.ajax? This seems like the case as the apostrophe is already encoded. jQuery must be encoding the ampersand?
- I'm saving my files with
UTF-8 Encoding. - MySQL charset:
UTF-8 Unicode (utf8) - Database collation
utf8_general_ci. - Using meta tag with HTML 5,
<meta charset="utf-8">. - Backend PHP script for ajax uses
json_encode()on array of property names. - Using
error_log()to store the result ofjson_encode()I get,[{"value":"Bill's Apartments"}] - Using
console.log(data)showsBill's Apartments. - Accessing the PHP Ajax script directly in the browser with
?term=biappended to the path returns[{"value":"Bill's Apartments"}]. - Using PHP functions like
html_entity_decode()orhtmlspecialchars()do not work or appear to be the proper solution.
HTML
<input type="text" id="property-name" name="property-name" value="">
jQuery
$('#property-name').autocomplete({
source: function(request, response) {
$.ajax({
url : ABSPATH + 'includes/ajax/property-name-search.php',
dataType : "json",
data : request,
success : function(data) {
response(data);
}
});
},
max: 25,
minLength: 2
});
Simplified Backend PHP
$query = "SELECT `property_name`
FROM `property_name_table`
WHERE `property_name` LIKE '%name%'";
$result = $mysqli->query( $query );
while ( $row = $result->fetch_object() ) :
$property_names[] = array( 'value' => $row->property_name );
endwhile;
echo json_encode( $property_names );