I believe the problem is with the .split () method. This does not work when characters match the beginning of a name or the end of a name.
Check out the JSFiddle here: http://jsfiddle.net/5uebxvex/
For reference, I am using the Employees table from the Northwind database. For example, one of the employees is Andrew Fuller . If you start with andr , this will produce the corresponding results, but it will not change the appearance of the results by adding bold, italics and underlining HTML formatting to the results. However, if you start by entering letters that match somewhere in the middle of the string, for example, drew , then it will return the correct results and add HTML formatting. In addition, as you can see from the PHP below, you can enter the full name of the person, for example rew ful , and it will display the correct matching employees. However, after adding a character, it will stop adding formatting. I really don't know why.
In addition to this problem, I noticed that if you quickly enter a text input field, this often leads to multiple results. For example, type andrew very quickly in the text box. Or try uller into the text box quickly. This will bring unique results until you get to the end, where it sometimes brings you multiples of the same results.
What's going on here? Can someone explain what the problem is?
Here PHP is northwind.php : (the problem is not in PHP - please check JSFiddle above)
<?php header('Content-Type: application/json'); header('Access-Control-Allow-Origin: *'); $db_user = 'user'; $db_pass = 'pass'; $db_name = 'name'; $db_host = 'host'; $conn = new mysqli($db_host, $db_user, $db_pass, $db_name); if ($conn->connection_error) { die('Connection error: '. $conn->connection_error); } $data = $_GET ? $_GET : $_POST; if (strlen($data['name']) > 0) { $sql = ' SELECT CONCAT(FirstName, " ", LastName) AS name, EmployeeID AS id FROM Employees WHERE FirstName LIKE "%'. $data['name'] .'%" OR LastName LIKE "%'. $data['name'] .'%" OR CONCAT(FirstName, " ", LastName) LIKE "%'. $data['name'] .'%" '; $result = $conn->query($sql) OR die('Query error: '. mysqli_error($conn)); if ($result->num_rows > 0) { $json = array(); while ($row = $result->fetch_assoc()) { $json[] = array('id' => $row['id'], 'name' => $row['name']); } print json_encode($json); } else { print '[]'; } } ?>
Check out the JSFiddle above for the rest of the code. Thank you in advance for information on the issues listed above.