EDIT
The script now is:
<script>
$('#tag').keyup(function() {
console.log($(this).val());
var termToSearch = $(this).val();
$(function() {
var availableTags;
$.ajax({
url: 'search_patient.php',
type: 'POST',
data: {term: termToSearch},
dataType: 'JSON',
success:function(output)
{
$.each( output, function(key, row)
{
availableTags = [row['patient_name']];
});
$( "#tags" ).autocomplete({
source: availableTags
});
}
});
});
});
</script>
I can see in the console values but still have not seen any auto-complete on the search text box.
END EDIT
I am trying to use jquery UI
library for auto-complete feature, but with an array filled using PHP and MySQL.
I started with the php and MySQL code, where I need to get patient names according to what I am typing in the search box (live auto-complete search)
<?php
//Set error reporting on
error_reporting(E_ALL);
ini_set("display_errors", 1);
//Include connection file
require_once('../include/global.php');
//Json and PHP header
header('Content-Type: application/json');
//Getting Value from text box
$term = '%'.$_POST['term'].'%';
//Array to get data into it
$response = array();
//Query
$searchPatient = "SELECT * FROM patient WHERE patient_name LIKE :term";
$searchStmt = $conn->prepare($searchPatient);
$searchStmt->bindValue(":term", $term);
$searchStmt->execute();
if($searchStmt->rowCount() > 0){
$output = $searchStmt->fetchall();
foreach ($output as $o){
$response['patient_name'] = $o['patient_name'];
}
return json_encode($response);
}
?>
In the page I included the jquery UI library, and according to their recommendation, they used the following:
<script src="../include/jquery-1.12.1.min.js"></script>
<script src="../include/jquery-ui.min.js"></script>
<script>
$(function() {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>
I can't figure out how to use $.ajax
to get the $response
array from PHP, and put it as availableTag = response.patient_name
I edited it to:
<script>
$(function() {
var availableTags;
var searchTerm = $("#tag").val();
$.ajax({
url: 'search_patient.php',
data: {term: searchTerm},
type: 'POST',
dataType: 'JSON',
success:function(response)
{
$.each( response, function(key, row)
{
availableTags = row['patient_name'];
});
$( "#tags" ).autocomplete({
source: availableTags
});
}
});
});
</script>
I had at the XHR term
as empty:
And I have this error:
Notice: Undefined index: term in C:\wamp\www\dentist\pages\search_patient.php on line 13
EDIT FOR Covic