I am using this source code : http://www.htmlblog.us/jquery-autocomplete in my project but I am not able to fetch the result when I type in the autocomplete textbox.
Where am I going wrong ?
I use the following code
Javascript : (I use a separate file for this javascript and checked if this calls using alert and I am able to generate alert messages at runtime.)
jQuery(document).ready
(
function()
{
$('#CourseId').autocomplete( { url:'AutoComplete_Courses.php', minLength:2 } );
}
);
Php:
<?php
echo('Your password must be at least 8 characters long');
if ( !isset($_REQUEST['term']) )
exit;
$dblink = mysql_connect('localhost', 'root', '') or die( mysql_error() );
mysql_select_db('mydatabase');
$rs = mysql_query('select courseid, coursename, creditpoints from coursedetails where coursename like "'. mysql_real_escape_string($_REQUEST['term']) .'%" order by coursename asc limit 0,10', $dblink);
$data = array();
if ( $rs && mysql_num_rows($rs) )
{
while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) )
{
$data[] = array(
'label' => $row['courseid'] .', '. $row['coursename'] .' '. $row['creditpoints'] ,
'value' => $row['courseid']
);
}
}
echo json_encode($data);
flush();
Looking at the documentation for the AutoComplete plugin. It looks like you used the wrong object property.
http://api.jqueryui.com/autocomplete/
As you can see, there is no ‘url‘ property, use ‘source‘ instead.
Change this line:
$('#CourseId').autocomplete( { url:'AutoComplete_Courses.php', minLength:2 } );to
$('#CourseId').autocomplete( { source:'AutoComplete_Courses.php', minLength:2 } );Additionally, whenever I’m having trouble with data coming from a database, I typically wrap the variable in PHP’s built-in output function var_dump() to see how PHP formatted the data.
https://www.php.net/manual/en/function.var-dump.php