dsxd62219570 2017-11-21 13:10
浏览 66
已采纳

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);
  • 写回答

1条回答 默认 最新

  • duannong1801 2017-11-21 13:29
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?