Autocomplete TextBox using php

10,448

Latest version of jQuery and -UI makes it work:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

$(document).ready(function() {
  $("#txtpname").autocomplete({
    source: [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ],
    minLength: 1
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/base/jquery-ui.css" />

<form method="POST">
  <input type="text" name="txtpname" id="txtpname" size="30" class="form-control" placeholder="Please Enter City or ZIP code">
</form>
Share:
10,448
Admin
Author by

Admin

Updated on June 17, 2022

Comments

  • Admin
    Admin about 2 years

    I am doing autocomplete using PHP but I'm getting an error with the following code so please help me.

    INDEX.PHP

    this is my HTML code

    <form method="POST">
        <input type="text" name="txtpname" id="txtpname" size="30" class="form-control" placeholder="Please Enter City or ZIP code">
    </form>
    

    this is my script

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
    <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" />
    
    <script type="text/javascript">
        $(document).ready(function(){
            $("#txtpname").autocomplete({
                source:'ajax_autocomplete_party.php',
                minLength:1
            });
        });
    </script>
    

    this is my ajax file from where i get data. ajax_autocomplete_party.php include "script/db.php";

    $term=$_GET["txtpname"];
    
     $query=mysql_query("SELECT * FROM party_details where NAME like '%".$term."%' order by NAME");
     $json=array();
    
    while($party=mysql_fetch_array($query))
    {
        $json[]=array(
            'value'=> $party["PARTY_ID"],
            'label'=>$party["NAME"]
        );
    }
    
    echo json_encode($json);
    

    I'm getting an error while my page reload error is: autocomplete not defined what to do now