jquery php json form automatically populate code

Source: Internet
Author: User
Tags extend json mysql tutorial php code php json php mysql php tutorial

This form of automatic completion function, such as Baidu, Google, Taobao their search function are available, when the user input a content to be based on the condition display related content.


<script>
;(function ($) {
/* plugin */
$.fn.autocomplete=function (o) {
    if (O.ajax) o.ajax=$.extend ({url: ', datatype: ' JSON ', async:true}, o.ajax| | {});
    o.elemcss Tutorial =$.extend ({focus:{' color ': ' #f00 '}, blur:{' color ': ' #000 '}}, o.elemcss| | {});
    o=$.extend ({
        source:null,/* privide an array For match */
        ajax:{},/* provide a AJAX conditions, if source is exist this parameter is invalid */
        input: ' input ',/* provide the selector of input box */
        popup: ' UL ',/* provide the selector of popup box, it must be a UL elemen T of HTML */
        elemcss:{}/* provide the focus and blur CSS objects of items I n The Popup Box */
   },o| | {});

var handler= (function () {
var handler=function (e,o) {return new handler.prototype.init (e,o);};
handler.prototype={
E:null, O:null, Timer:null, show:0, $input: null, $popup: null,
Init:function (e,o) {
this.e=e;
This.o=o;
this. $input =$ (THIS.O.INPUT,THIS.E);
this. $popup =$ (THIS.O.POPUP,THIS.E);
This.initevent ();
},
Match:function (Quickexpr,value,source) {
for (var i in source) {
if (value.length>0 && quickexpr.exec (source[i))!=null)
this. $popup. Append (' <li><a href= ' page special effects:; " > ' +source[i]+ ' </a></li> ');
}
if ($ (' Li a ', this. $popup [0]). length) {this. $popup. Show ();} else{this. $popup. Hide ();
},
Fetch:function (ajax,search,quickexpr) {
var that=this;
$.ajax ({
Url:ajax.url+search,
Datatype:ajax.datatype,
Async:ajax.async,
Error:function (data,es,et) {alert (' Error ');},
Success:function (data) {That.match (quickexpr,search,data);}
});
},
Initevent:function () {
var that=this;
this. $input. focus (function () {
var value=this.value, Quickexpr=regexp (' ^ ' +value, ' I '), self=this;
That.timer=setinterval (function () {
if (Value!=self.value) {
Value=self.value;
that. $popup. HTML (");
if (value!= ') {
Quickexpr=regexp (' ^ ' +value);
if (That.o.source) That.match (Quickexpr,value,that.o.source);
else if (That.o.ajax) That.fetch (that.o.ajax,value,quickexpr);
}
}
},200);
}). blur (function () {
Clearinterval (That.timer);
var current=-1;
var len=that. $popup. Find ("Li a"). Length-1;
$ ("Li a", that. $popup [0]). Click (function () {
that. $input [0].value=$ (This). text ();
that. $popup. HTML ('). Hide ();
). focus (function () {
Current = $ (this). Parent (). index ();
$ (this). CSS (That.o.elemcss.focus);
}). blur (function () {
$ (this). CSS (That.o.elemcss.blur);
});
$ ("Li a", that. $popup [0]). KeyDown (function (event) {
if (event.keycode==40) {
current++;
if (current<0) Current=len;
if (Current>len) current=0;
That's $popup. Find ("Li a").
}else if (event.keycode==38) {
current--;
if (Current>len) current=0;
if (current<0) Current=len;
That's $popup. Find ("Li a").
}
});
}). KeyDown (function (event) {
if (event.keycode==40) {
$popup. blur (). Find ("Li a"). Get (0). focus ();
}
});
$ (THIS.E). Hover (function () {that.show=1;},function () {that.show=0;});
$ (document). Click (function () {if (that.show==0) {that. $popup. Hide ();}});
}
};
handler.prototype.init.prototype=handler.prototype;/* jquery Style * *
return handler;
})();

Return This.each (function () {handler (this,o);});
};
/* Invoke * *
$ (document). Ready (function () {
$ (". AutoComplete"). AutoComplete ({
SOURCE:[123,1234,43563,12346,3464564,3454,7567,956,456,9383,893,999],
ajax:{URL: './php tutorial/fetch.php?search= ', datatype: ' JSON ', async:false},
elemcss:{focus:{' color ': ' #0f0 '}, blur:{' color ': ' #f00 '}}
});
});
* Conclude * *
}) (jquery);

</script>

PHP code

<?php
Class datafetch{
Private $conn, $rs;
function __construct () {
$this->conn = mysql tutorial _connect ("localhost", "root", "pwd") or Die ("cant ' t connect host~");    mysql_select_db ("Studentinfo", $this->conn) or Die ("Cant ' t select database~"); }
Public function data_list ($s) {
$this->rs=mysql_query ("Select Sno from student where Sno like ' $s%") or Die ("Can ' t fetch~");
$datalist =array ();
while ($row = mysql_fetch_array ($this->rs,mysql_assoc))
{
Array_push ($datalist, $row);
}
return $datalist; }}
$search = Isset ($argv [1])? $ARGV [1]: (Isset ($_get[' search '])? $_get[' search ': ');
$fetch =new Datafetch ();
$data = $fetch->data_list ($search);
Echo ' [';
foreach ($data as $key => $value)
{echo "NT";
Echo ' "'. $value [' Sno ']. '";
if ($key!=count ($data)-1) echo ', '; else echo "n";} echo "]n";
?>


This is the use of PHP MySQL to return JSON data, because the above jquery is also the analysis of JSON data to place, because the JSON data is relatively small and fast, so many times use it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.