I have following auto-complete code to call PHP file via AJAX and get the results in JSON form:
$( "#autocomplete_customer" ).on( "listviewbeforefilter", function ( e, data ) {
var $ul = $( this ),
$input = $( data.input ),
value = $input.val(),
html = "";
$ul.html( "" );
if ( value && value.length > 0 ) {
$ul.html( "<li><div class='ui-loader'><span class='ui-icon ui-icon-loading'></span></div></li>" );
$ul.listview( "refresh" );
$.ajax({
url: "SearchCustomer.php",
dataType: "jsonp",
crossDomain: true,
data: {
term: $input.val()
}
})
.then( function ( response ) {
$.each( response, function ( i, val ) {
html += "<li>" + val + "</li>";
});
$ul.html( html );
$ul.listview( "refresh" );
$ul.trigger( "updatelayout");
});
}
});
Here is SearchCustomer.php file:
<?php
include "config.php";
mysql_select_db($db, $con);
$search = $_GET['term'];
$result = mysql_query("my SELECT query on basis of $search") or die('Something went wrong');
$json = '[';
$first = true;
while ($row = mysql_fetch_assoc($result))
{
if (!$first) { $json .= ','; } else { $first = false; }
$json .= ".$row['cli_razon_social'].";
}
$json .= ']';
echo $json;
?>
I am facing two issues:
Results are not returning from PHP file or I am doing it wrong. Please help me to fix it.
Currently user cannot select a value from auto-complete list. How can I modify this script to let user select auto-complete value from text field auto-complete
ullist?
Thanks.