php jquery autocomplete

來源:互聯網
上載者:User
在網上搜了許多,累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; i1)){                $("#u_name").blur();            }                    }    });});

3. 後台PHP部分:

finduser.php

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);}?>

4. 前台頁面就一輸入框,主要是以上PHP的返回及JS的處理。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.