简体   繁体   中英

PHP response using JQuery

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

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

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

<?php

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();
    $this->doDisplay();
    $this->hookup->close(); 

}

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;

        $result->close();


    }
    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() {

$this->display();   

}



public function display() {

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

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

    echo $this->json;

}
}

$Business = new swapnil();

thanks in advance.

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

$(this).autocomplete({
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 .

thanks for all your inputs. I got the error. It was happening as i had other echo - Successful connection to MySQL - in one of the classes. json was finding hard to send the entire date. I commented that line and worked fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM