Typeahead not recognizing accents

25 Views Asked by At

I am using typeahead when people use a form on my website.

This is where they fill the form

People type the first few letters of a city name and I return possible matching results. I have the city names in a database.

The issue is when the city starts with an accent "é" for example, I get no results. I should get results because I have city names that start with a "é" in my database. Here is an example:

not working "élan" should return "élancourt". If I write "elan" it doesn't work either, because in my database it's "élancourt" probably.

If there is an accent in the word it works, it only fails when the word starts with an accent. typeahead working

So this is the code for typeahead:

<script>
      $(document).ready(function () {
        $('#code_postal_ville').typeahead({
          minLength: 3,
          highlight: true,
        },
          {
            name: 'villes',
            source: function (query, syncResults, asyncResults) {s
              var tableName = 'villes_france_free';
              console.log('Table Name:', tableName);
              $.ajax({
                url: '<?php echo get_template_directory_uri(); ?>/get_cities.php',
                method: 'GET',
                data: { query: query, table: tableName },
                dataType: 'json',
                success: function (data) {
                  var uniqueCities = new Set();

                  data.suggestions.forEach(function (item) {
                    if (item.toLowerCase().indexOf(query.toLowerCase()) === 0) {
                      uniqueCities.add(item);
                    }
                  });
                  var filteredData = Array.from(uniqueCities);
                  asyncResults(filteredData);
                }
              });
            }
          });
      });

    </script>

and this is the "get_cities.php" file

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test_leo";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if (isset($_GET['query']) && isset($_GET['table'])) {
    $query = $_GET['query'];
    $table = $_GET['table'];

    $sql = "SELECT ville_nom_reel FROM $table WHERE ville_nom_reel LIKE ?";
    $stmt = $conn->prepare($sql);
    $param = "%" . $query . "%";
    $stmt->bind_param("s", $param);
    $stmt->execute();
    $result = $stmt->get_result();

    $cities = array();
    while ($row = $result->fetch_assoc()) {
        $cities[] = $row['ville_nom_reel'];
    }
    
    $uniqueCities = array_unique($cities);
    
    $filteredCities = array_filter($uniqueCities, function($item) use ($query) {
        return stripos($item, $query) === 0;
    });
    
    $response = new stdClass();
    $response->suggestions = array_values($filteredCities);
    
    echo json_encode($response);
}
$conn->close();
?>

I tried libraries like unidecode or diacritics. But I probably used them wrong because it wouldn't work.

0

There are 0 best solutions below