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

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)