top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Implementing setAutoComplete in PHP/JQuery

+2 votes
1,866 views

I am implementing a feature in QueryHome itself so thought what could be the best way to implement and found the best way could be to get the help people. Following is what I am trying to do -

  1. I have a form with multiple field (say 10 fields)
  2. For two fields I want to implement autocomplete feature.
  3. Once user enters 3 characters autocomplete should get hit after that on each character typed..
  4. A PHP function should be called which will set json_encode to populate the result.

Any pointer...

posted Oct 24, 2013 by Salil Agrawal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Following link looks useful
http://phpseason.wordpress.com/2013/02/13/php-autocomplete-tutorial-using-jquery/

Here minlength can be set to 3
Thanks Pankaj, it should be helpful...
Yes it worked, would you like to put the content in more readable form for the benefit of People.

1 Answer

+1 vote
 
Best answer

PHP GUI Code where auto-complete function would be called

<html><head>
<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(){
            $("#name").autocomplete({
                source:'getautocomplete.php',
                minLength:1
            });
        });
</script>
</head> 
 <body> 
      <form method="post" action="">
                Name : <input type="text" id="name" name="name" />
      </form></body>

Implementation of database interaction code

<?php
     mysql_connect("localhost","root","");
     mysql_select_db("php_autocomplete");

     $term=$_GET["term"];

     $query=mysql_query("SELECT * FROM student where name like '%".$term."%' order by name ");
     $json=array();

      while($student=mysql_fetch_array($query)){
             $json[]=array(
                'value'=> $student["name"],
                'label'=>$student["name"]." - ".$student["id"]
                    );
       }

      echo json_encode($json);
   ?>
answer Oct 26, 2013 by Pankaj Agarwal
Similar Questions
+1 vote

I use following code for jQuery.ajax to call php.

$.ajax('myserver.php")

It seems that one php file only work for one jquery ajax code, Is it possible to have more than one function or one return different result in one php file?

Your help is greatly appreciated,

+1 vote

I have a POST form and action itself like following

I want to show success message when POST complete or error message if there is any. I would like to know are there any property or global variable I can check to show message to users. If not, it seems that the only solution is jQuery, since it has success function that jQuery call after POST.

...