0

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);
2
  • Soo many things are wrong with this code..read up on best php practices. In the meantime, please don't go live with this Commented Nov 21, 2017 at 13:14
  • i am working on local can you please find a solution to this problem? Commented Nov 21, 2017 at 13:16

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

7 Comments

Are you sure the the sql it's correct? the code i insert work if the json encode is not emptyi tried now and it's work control the query on the db...[$i]['item'] in javascript is the key item in the json php...use mysqli_fetch_assoc not array.... and don't use mysql it's deprecated use mysqli ;)
yes because when i am trying to get the results via query string i am getting
i have uploaded the image you can see it
well so in the data of the ajax insert data:{term: request.term}, and use my mysqli_fetch_assoc and $data[] = array('item'=>$row['lemma'])
can you edit your answer here it will be very helpful for me i am working from 10 hours and stucked :(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.