ayyyymtl ayyyymtl - 10 days ago 4
Ajax Question

Autocomplete form once the name is enter PHP

I found a couple of similar questions but nothing with the same "variables" so here we go:

I have a simple form where I enter a name and I need the rest of the form to be completed automatically (ID).

Fetching the data in the DB is not an issue, I'm able to return Json using PHP (Search.php):

//get search term
$searchTerm = $_GET['term'];

//get matched data from table
$query = $db->query("SELECT * FROM info_employer WHERE Prenom LIKE '%".$searchTerm."%' ORDER BY Prenom ASC");
while ($row = $query->fetch_assoc()) {
$data[] = array( Nom => $row['Prenom'] . ' ' . $row['Nom'], Num => $row['Num'] );

//return json data
echo json_encode($data);

This give me an array that look like this:

[{"Nom":"Andre Nadeau","Num":"104J"},{"Nom":"Andre Potvin","Num":"130J"},{"Nom":"Andre Thibodeau","Num":"91J"}]

Friends told me that I should use Jquery and Ajax to use this array to fill my form but I have 2 issue.

First If I return an Array instead of "just the name" my autocomplete form don't work anymore. It give me X numbers of blank space (depending the number of results).

And of course my biggest problem is that i'm not able to send the ID (Num) in the form

Javascript i'm using to autocomplete the name :

$(function() {
$( "#Nom" ).autocomplete({
source: 'Search.php',


You need to change the return object to make it match the spec here

$data[] = array( "label" => $row['Prenom'] . ' ' . $row['Nom'], "value" => $row['Num'] );

This should result in an array of objects with a 'label' and 'value' key. This will work with your autocomplete.