Home > Backend Development > PHP Tutorial > php jquery autocomplete

php jquery autocomplete

WBOY
Release: 2016-06-23 14:36:09
Original
1217 people have browsed it

在网上搜了许多,累shi了,折腾了半天,终于搞定,总结一下下...

1. 需要jquery-1.3.2.min.js (++),jquery.autocomplete.js,jquery.autocomplete.css 三个文件。哪里有?google..

2. 前端页面 js 如下:

user.js

$(function() {    $("#u_name").blur(function(){        //查询user表,检查是否存在,存在,则将用户信息显示;不存在,刚更新隐藏域,标识为new        //如果是新添加的用户,保存到数据库。        var u_name = $.trim($("#u_name").val());        if(u_name != ""){            $.ajax({                url:"finduser.php",                type:"get",                dataType:"json",                data:{username:$("#u_name").val(),u:"definate_text"},                error:function(){                    sessionTimeOut();                },                success:function(data){                    if(data!=null){                        $("#u_mail").attr("value",data.mail);                        $("#u_phone").attr("value",data.phone);                        $("#u_office").attr("value",data.office);                        $("#existflag").attr("value",data.existflag);                    }                }            });            }    });                //here     $("#u_name").autocomplete("finduser.php", {        cacheLength:0,        selectFirst:true,        matchSubset:false,        multiple: false,             width:135,             max:20,             dataType: 'json',        extraParams: {username:function(){return $("#u_name").val();},u:"indefinate_text"},        parse: function(data) {             var rows = [];              if(""!=data){                for(var i=0; i<data.length; i++){                       rows[rows.length] = {data:data[i],result:data[i]};                    }            }            return rows;           }    });         $(document).keydown(function(event){        if(event.keyCode==13){            if((document.activeElement.id=="u_name") && ($.trim($("#u_name").val()).length>1)){                $("#u_name").blur();            }                    }    });});
Copy after login

3. 后台PHP部分:

finduser.php

<?php$flag= $_GET['u'];if($flag=='definate_text'){    $sql = "select * from $users_table where user_name = '".$_GET['username']."'";    $autoresult = $db->query($sql);    $autorow = $db->fetch_array($autoresult);    $autoemail = $autorow[email];    $autooffice = $autorow[office];    $autophone = $autorow[phone];    $existflag=1;    if ($autorow==false){        $existflag=0;//0不存在,1存在。    }    $array=array('mail'=>"$autoemail",'office'=>"$autooffice",'phone'=>"$autophone",'existflag'=>"$existflag");    echo json_encode($array);}if($flag=='indefinate_text'){    $sql = "select user_name from $users_table where user_name like '".$_GET['username']."%'";    $autoresult = $db->query($sql); //$db是创建好的mysql_connect,query方法其实调用的是mysql_query    $array=array();    while($row = $db->fetch_array($autoresult)){        array_push($array,array($row['user_name']));    }    echo json_encode($array);}?>
Copy after login

4. 前台页面就一输入框,主要是以上PHP的返回及JS的处理。


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template