jQuery Autocomplete (Remote) - example

前端 未结 4 978
栀梦
栀梦 2020-12-16 03:49

I was really hoping to avoid posting a new question, but I cannot find a functioning example of the jQuery Autocomplete Remote feature that includes both the calling page an

4条回答
  •  旧时难觅i
    2020-12-16 04:20

    Here is the correct code for search.php:

        $conn = mysql_connect("localhost", "USERNAME", "PASSWORD");
        mysql_select_db("DATABASE", $conn);
        $q = strtolower($_GET["term"]);
    
    $return = array();
        $query = mysql_query("select FIELD from TABLE where FIELD like '%$q%'");
        while ($row = mysql_fetch_array($query)) {
        array_push($return,array('label'=>$row['FIELD'],'value'=>$row['FIELD']));
    }
    echo(json_encode($return));
    

    Some key points

    • The word term is nowhere in the sample calling page given by jqueryui, but this is the Querystring name used
    • You must create an array of the value, and then json encode before returning

    I hope this helps some folks out in the future!

提交回复
热议问题