简体   繁体   English

使用JQuery的PHP响应

[英]PHP response using JQuery

I am trying to use autocomplete using PHP class. 我正在尝试通过PHP类使用自动完成功能。 Below is JQuery. 下面是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 我在LiveSearch.php下面创建了从数据库获取数据的方法

<?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. 当我在网络浏览器上打开LiveSearch.php时,它会为我提供正确的结果。通过从数据库中获取数据来提供正确的json输出。 But when i try to use autcomplete search it doesn't give any result. 但是,当我尝试使用autcomplete搜索时,它不会给出任何结果。 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. 我以为结果不会转到JQuery,但是当我编写其他简单类(如下所示)并创建该类的实例时,结果会显示在自动完成中。 Not sure if anything is missing in the LiveSearch class, but I am getting result for LiveSearch.php directly on browser. 不知道LiveSearch类中是否缺少任何内容,但是我直接在浏览器上得到了LiveSearch.php的结果。 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. 其次,我认为问题出在返回的JSON上,因为您指出第二次尝试使用示例数据是可行的。

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 . 之后,您应该调试getJSON源响应并将其格式化以匹配自动完成格式

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. 发生这种情况的原因是,在其中一个类中,我还有其他回声-成功连接到MySQL。 json was finding hard to send the entire date. json很难发送整个日期。 I commented that line and worked fine. 我评论了那条线,并且工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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