jQuery Ui自动完成没有从Php MySQL文件中获取价值

jQuery Ui自动完成没有从Php MySQL文件中获取价值

问题描述:

I am using jquery ui auto complete but when i am trying to get the value from simple javascript variable i am getting but jquery Ui auto complete is not working with the php mysql file this is my code

enter image description here

<div class="col-md-12" class="col-centered">

                   <input id="tags" type="text" class="dic_input ui-autocomplete-input" data-provide="tags" name="ajaxData"/>
                   <button class="btn btn-lg btn-default"><i class="fa fa-2x fa-search"></i></button>
            </div>

this is my javascript code

$(function() 
{ var availableTags = [
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ];
    $("#tags").autocomplete({
        source: "auto.php",
        minLength: 1
    });
});

this is my php code

$db=mysql_connect("localhost","root","");
    mysql_select_db("hifzil");

    $searchTerm = $_GET['term'];

    //get matched data from skills table
    $sql ="SELECT lemma FROM lemma WHERE lemma like '" . $searchTerm . "%' ORDER BY lemma LIMIT 0,6";

    $q=mysql_query($sql);
    while ($row = mysql_fetch_array($q)) {
        $data[] = str_replace("-"," ",$row['lemma']);

    }

    //return json data
    echo json_encode($data);

我正在使用jquery ui auto complete但是当我试图从简单的javascript变量获取值时我得到但是 jquery Ui auto complete不能使用php mysql文件这是我的代码 p>

p>

  &lt; div class =“col-md-12”class =“col-centered”&gt; 
 
&lt; input id =“tags”type =“text”class =“dic_input ui-autocomplete-input  “data-provide =”tags“name =”ajaxData“/&gt; 
&lt; button class =”btn btn-lg btn-default“&gt;&lt; i class =”fa fa-2x fa-search“&gt;  &lt; / i&gt;&lt; / button&gt; 
&lt; / div&gt; 
  code>  pre> 
 
 

这是我的javascript代码 p>

   $(function()
 {var availableTags = [
“ColdFusion”,
“Erlang”,
“Fortran”,
“Groovy”,
“Haskell”,
“Java”  ,
“Ja  vaScript“,
”Lisp“,
”Perl“,
”PHP“,
”Python“,
”Ruby“,
”Scala“,
”Scheme“
]; 
  $(“#tags”)。autocomplete({
 source:“auto.php”,
 minLength:1 
}); 
}); 
  code>  pre> 
 
  

这是我的php代码 p>

  $ db = mysql_connect(“localhost”,“root”,“”); 
 mysql_select_db(“hifzil”); 
  
 $ searchTerm = $ _GET ['term']; 
 
 //从技能表中获取匹配的数据
 $ sql =“SELECT lemma FROM lemma WHERE lemma like'”。  $ searchTerm。  “%'ORDER BY引理LIMIT 0,6”; 
 
 $ q = mysql_query($ sql); 
 while($ row = mysql_fetch_array($ q)){
 $ data [] = str_replace(“ -  “,”“,$ row ['lemma']); 
 
} 
 
 //返回json数据
 echo json_encode($ data); 
  code>  pre> 
 

It's impossible that this work, first you are doing avaiableTags in local but after at the source you put a remote source, your example it's the basic that you find in jquery but there the example is in local so at least source:avaiableTags, if you want to do a remote call you can use the code under but open with f12 the console log of the browser and after put your own of select item

$("#tags").autocomplete({
    source:function(request,response){
        $.ajax({
          type: "GET",
          data:{},
          url: "auto.php",
          dataType: "json",
          success:function(data){
               console.log(data);                         
           }
         });                                        
    },
    select: function(event,ui){},//your own at the selct item 
    change: function(event,ui){},//your own in change event
    response: function(event, ui) {}//your own in response 
    minLength: 1
});

and in the php i think the array is not correct... you must write

        $data[] = array('item'=>str_replace("-"," ",$row['lemma']))

And after in succe javascript you can write

                          success:function(data){

                                 items= data;
                                 listItem=[];
                                 for (i=0; i<items.length; i++){
                                   listItem[i] = items[i]['item'];
                                 }
                               response(listItem);                                
                           }

and the response will be the list of the items after in select event you can do your own at selection...etc..etc..but must use the console browser to see the answer.

Javascript :

$("#tags").autocomplete({
    source:function(request,response){
        $.ajax({
          type: "GET",
          data:{term: request.term},
          url: "auto.php",
          dataType: "json",
          success:function(data){
                             items= data;
                             listItem=[];
                             for (i=0; i<items.length; i++){
                               listItem[i] = items[i]['item'];
                             }
                           response(listItem);                        
           }
         });                                        
    },      
    minLength: 1
});

Php:

$db=mysqli_connect("localhost","root","","hifzil");

$searchTerm = $_GET['term'];

//get matched data from skills table
$sql ="SELECT lemma FROM lemma WHERE lemma like '" . $searchTerm . "%' ORDER BY lemma LIMIT 0,6";
$data = array();
$q=mysqli_query($db,$sql);
while ($row = mysqli_fetch_assoc($q)) {
    $lemma = str_replace("-"," ",$row['lemma']);
    $data[] = array('item'=>$lemma);

}

//return json data
$result = json_encode($data);
print $result;