Benny64 Benny64 - 4 months ago 7
Ajax Question

No suggestion is generated when I type in auto-suggestion search box

I'm working on a search form that provides suggestions as you type. It works fine when I draw the data from a single column in the result returned by MySQL database. When I type in LastName, it produces suggestions. But when I try to concat two columns (LastName and FirstName), no suggestion is produced. Here is my code:

******* THIS ONE WORKS FINE AS SUGGESTIONS ARE DISPLAYED WHEN I TYPE *******

<?php
Include ('connection_script.php');

// Get matched data from table
$query = $conn->query("SELECT LastName FROM friends_contact_table WHERE LastName LIKE '%".$searchTerm."%' ORDER BY LastName ASC");

// Fetch result from column by association
While ($row = $query->fetch_assoc() ) { $data[] = $row['LastName']; }

// Return JSON data
Echo json_encode($data);
?>


******* NO SUGGESTION IS DISPLAYED WHEN I CONCAT TWO COLUMNS *******

<?php
Include ('connection_script.php');

// Get matched data from table
$query = $conn->query("SELECT CONCAT(LastName," ",FirstName) as Output FROM friends_contact_table WHERE LastName LIKE '%".$searchTerm."%' ORDER BY LastName ASC");

// Fetch result from column by association
While ($row = $query->fetch_assoc() ) { $data[] = $row['Output']; }

// Return JSON data
Echo json_encode($data);
?>


Here's the function on the search form page to pull suggestions and display as dropdown

<Script>
$(function() {
$( "#searchbox" ).autocomplete ({ source: 'search_script.php' });
});
</script>


How do I make suggestions like "Apple Mango","Apple Pears","Avocado Kiwi" appear as suggestion in the search box instead of single names? I'm still a beginner. Pardon me if my question isn't correctly constructed, but I think someone is getting what I'm trying to say. Thanks.

Answer

I think this is work properly

$query = $conn->query("SELECT CONCAT(fname, ' ', lname) as Output FROM friends_contact_table WHERE LastName LIKE '%".$searchTerm."%' ORDER BY LastName ASC");