Swapnil Ranadive Swapnil Ranadive - 1 year ago 58
Javascript Question

PHP response using JQuery

I am trying to use autocomplete using PHP class. Below is JQuery.

$(document).ready(function() {
$( ".autocomplete" ).each(function() {
source: 'php/LiveSearch.php?name='+ $(this).attr('name') + '&',
minLength: 1,
appendTo: "#container"

I have created below LiveSearch.php to get the data from database


interface IConnectInfo {

const HOST = 'XXX.0.0.1:XXXX';
const UNAME = 'root';
const PW = 'xxx';
const DBNAME = 'test';

public static function doConnect();


class UniversalConnect implements IConnectInfo{

private static $server=IConnectInfo::HOST;
private static $currentDB=IConnectInfo::DBNAME;
private static $user=IConnectInfo::UNAME;
private static $pass=IConnectInfo::PW;
private static $hookup;

public static function doConnect() {

self::$hookup = mysqli_connect(self::$server, self::$user, self::$pass, self::$currentDB);
if(self::$hookup) {
echo ("Successful connection to MySQL:<br/>");
elseif (mysqli_connect_error(self::$hookup)) {

echo ('Here is what is failed: ' .mysqli_connect_error());
return self::$hookup;



class LiveSearch {

private $table;
private $hookup;
private $sql;
private $name = array();
private $res;
private $json;
private $term;

public function __construct() {

$this->table = "master";
//create mysqli object
$this->hookup = UniversalConnect::doConnect();


public function doDisplay() {

$this->term = 'g';

//$this->term = $_GET['term'];

$this->sql = "SELECT DISTINCT(Asset) FROM $this->table where Business LIKE '%$this->term%'";

try {

$result = $this->hookup->query($this->sql);

while ($row = $result->fetch_assoc()) {

$this->res = $row['Asset'];
array_push($this->name, $this->res);


$this->json = json_encode($this->name);

echo $this->json;


catch(Exception $e) {

echo "Here's what went wrong: " .$e->getMessage();



$Business1 = new LiveSearch();


when i open LiveSearch.php on web browser, it gives me correct results..giving proper json output by fetching data from database. But when i try to use autcomplete search it doesn't give any result. i thought result is not going to JQuery but when i wrote other simple class (given below) and created instance of that class, result is appearing in autocomplete. Not sure if anything is missing in the LiveSearch class, but I am getting result for LiveSearch.php directly on browser.
below is other simple class that is working

class swapnil {

public function __construct() {



public function display() {

$this->name = array("Swapnil");

$this->json = json_encode($this->name);

echo $this->json;


$Business = new swapnil();

thanks in advance.

Answer Source

I think that it is more readable if you create a separate function for getting the data from the server, for example:

source: function(request, response) {
            $.getJSON("php/LiveSearch.php", { name: $('#yourInputId').val() }, response);

Secondly I think that the problem is with your returned JSON, beacuse you pointed out that your second try with sample data works.

So, use a function instead a string for your source (because it's more readable and it's easier to debug). After that you should debug your getJSON source response and format it to match the autocomplete format.