I have a small code snippet below. I'm using jQueryUI's autocomplete function to pull data from my mySql data base and populate a user name into search bar when someone searches for a specific user. I want it to autocomplete the first and last name of the person being searched for. For example, if the autocomplete function predicts that someone is looking for Lebron James, I want it to return both the first and last name. Currently, I can only get it to return the first name. From looking at the documentation, I thought I could add multiple key and value pairs by putting them one after another. This only produces one name for me. I added a "2" after the second label/value pair names to see if giving them different names would help. It still only returns the first name and not the last name. How can I get it to return both the first and last name in my autocomplete?
my index.php
<?php
// here we set the database information
$SERVER = 'localhost';
$USER = 'user';
$PASS = 'password';
$DATABASE = 'database';
// we are instantiating the mysqli database object
$db = new mysqli($SERVER, $USER, $PASS, $DATABASE);
// error check
if($db->connect_errno > 0){
// report an error
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = 'SELECT * FROM users WHERE user LIKE "' . $_REQUEST['term'] . '%"';
$result = $db->query($sql);
$data = array();
while ($row = $result->fetch_assoc())
{
$data[] = array(
'label' => $row['firstName'] ,
'value' => $row['lastName'],
'label2' => $row['lastName'],
'value2' => $row['lastName'],
);
}
// jQuery wants JSON data
echo json_encode($data);
flush();
my html and js
<form id="searchbox" action="">
<input id="search" type="text" placeholder="Search">
<!--<input id="submit" type="submit" value="Search">-->
</form>
<script type="text/javascript" >
//script for autocompleting usernames when being search
$('#search').autocomplete(
{source:'autosuggest.php',
minLength:2
});
</script>