PHP自动补全表单的两种方法


Posted in PHP onMarch 06, 2017

效果图:

第一种:从数据库中检索之后补全

PHP自动补全表单的两种方法

第二种:邮箱等纯前端的补全

PHP自动补全表单的两种方法

先说第二种,使用开源的插件,所以相对简单。

github上面的项目 completer。

https://github.com/fengyuanchen/completer 做法特别容易,github上面有详细的文档。

一开始尝试用这个来配上自己的后台代码,做成第一种的自动补全,搞了半天失败了。可能本人js太差,改动太多的话,代码很复杂,除非认真研究上面这个开源项目。

主要失败在我在后台数据库找出来的完整的模糊查询得到的数据,用上面的插件只能是补全在后面,没有办法完全替换输入值。比如你输入fafe,查到fafegeg, 但是下拉的表项中只能选到 fafefafegeg,因为是补全。

下面说说第一个怎么做,百度不到什么好的经验介绍,就自己按自己的思路实现了:

/**
  * php 返回模糊搜索的结果给自动完成
  */
 public function actionComplete($value,$blocked) {
  //将输入的值与用户名和邮箱进行模糊查询
  $result1 = User::find()->where(['blocked'=>$blocked])->andWhere(['type'=>$this->type])->andWhere(['like','username',$value])->all();
  $result2 = User::find()->where(['blocked'=>$blocked])->andWhere(['type'=>$this->type])->andWhere(['like','email',$value])->all();
  $string = '';
  foreach ($result1 as $v) {
   $string = $string.$v->username.',';
  }
  foreach ($result2 as $v) {
   $string = $string.''.$v->email.',';
  }
  $string = $string.'';
  //返回格式 类似username1,username2,email1,
  return $string ;
 }
//前端
 <input type="text" id="o" onkeyup="lookup(event,this.value);" placeholder="按用户名和邮箱检索" name="value">
         <div class="auto_hidden" id="auto"><!--自动完成 DIV--></div>
         <button type="submit" style="height: 30px;width: 55px" class="btn">检索</button>
//JS
<link href="<?= BACKEND_CSS_URL ?>/autoComplete.css" rel="external nofollow" rel="stylesheet" />
<script src="<?= BACKEND_JS_URL ?>/autoComplete.js"></script>
<script language="javascript">
 function lookup(event,inputString) {
  if(inputString.length != 0) {
   var blocked = '0';
   var request = new XMLHttpRequest();
   request.open("GET", "<?= PRE_BACKEND_URL ?>user/complete&value=" + inputString + "&blocked=" + blocked);
   request.send(null);
   request.onreadystatechange = function () {
    if (request.readyState === 4) {
     if (request.status === 200) {
      var result = request.responseText;
      ss = result.split(",")
      var autoComplete=new AutoComplete('o','auto',ss);
      autoComplete.start(event,ss);
     }
    }
   }
  }
 } // lookup
</script>
//autoComplete.css
.auto_hidden {
 width:204px;border-top: 1px solid #333;
 border-bottom: 1px solid #333;
 border-left: 1px solid #333;
 border-right: 1px solid #333;
 position:absolute;
 display:none;
}
.auto_show {
 width:204px;
 border-top: 1px solid #333;
 border-bottom: 1px solid #333;
 border-left: 1px solid #333;
 border-right: 1px solid #333;
 position:absolute;
 z-index:9999; /* 设置对象的层叠顺序 */
 display:block;
}
.auto_onmouseover{
 color:#ffffff;
 background-color:highlight;
 width:100%;
}
.auto_onmouseout{
 color:#000000;
 width:100%;
 background-color:#ffffff;
}
//autoComplete.js
/*
 通用: 自动补全(仿百度搜索框)
 */
var $ = function (id) {
 return "string" == typeof id ? document.getElementById(id) : id;
}
var Bind = function(object, fun) {
 return function() {
  return fun.apply(object, arguments);
 }
}
function AutoComplete(obj,autoObj,arr){
 this.obj=$(obj);  //输入框
 this.autoObj=$(autoObj);//DIV的根节点
 this.value_arr=arr;  //不要包含重复值
 this.index=-1;   //当前选中的DIV的索引
 this.search_value=""; //保存当前搜索的字符
}
AutoComplete.prototype={
 //初始化DIV的位置
 init: function(){
  this.autoObj.style.left = this.obj.offsetLeft + "px";
  this.autoObj.style.top = this.obj.offsetTop + this.obj.offsetHeight + "px";
  this.autoObj.style.width= this.obj.offsetWidth - 2 + "px";//减去边框的长度2px
 },
 //删除自动完成需要的所有DIV
 deleteDIV: function(){
  while(this.autoObj.hasChildNodes()){
   this.autoObj.removeChild(this.autoObj.firstChild);
  }
  this.autoObj.className="auto_hidden";
 },
 //设置值
 setValue: function(_this){
  return function(){
   _this.obj.value=this.seq;
   _this.autoObj.className="auto_hidden";
  }
 },
 //模拟鼠标移动至DIV时,DIV高亮
 autoOnmouseover: function(_this,_div_index){
  return function(){
   _this.index=_div_index;
   var length = _this.autoObj.children.length;
   for(var j=0;j<length;j++){
    if(j!=_this.index ){
     _this.autoObj.childNodes[j].className='auto_onmouseout';
    }else{
     _this.autoObj.childNodes[j].className='auto_onmouseover';
    }
   }
  }
 },
 //更改classname
 changeClassname: function(length){
  for(var i=0;i<length;i++){
   if(i!=this.index ){
    this.autoObj.childNodes[i].className='auto_onmouseout';
   }else{
    this.autoObj.childNodes[i].className='auto_onmouseover';
    this.obj.value=this.autoObj.childNodes[i].seq;
   }
  }
 }
 ,
 //响应键盘
 pressKey: function(event){
  var length = this.autoObj.children.length;
  //光标键"↓"
  if(event.keyCode==40){
   ++this.index;
   if(this.index>length){
    this.index=0;
   }else if(this.index==length){
    this.obj.value=this.search_value;
   }
   this.changeClassname(length);
  }
  //光标键"↑"
  else if(event.keyCode==38){
   this.index--;
   if(this.index<-1){
    this.index=length - 1;
   }else if(this.index==-1){
    this.obj.value=this.search_value;
   }
   this.changeClassname(length);
  }
  //回车键
  else if(event.keyCode==13){
   this.autoObj.className="auto_hidden";
   this.index=-1;
  }else{
   this.index=-1;
  }
 },
 //程序入口
 start: function(event,result){
  if(event.keyCode!=13&&event.keyCode!=38&&event.keyCode!=40){
   this.init();
   this.deleteDIV();
   this.search_value=this.obj.value;
   var valueArr=this.value_arr;
   //valueArr.sort();
   if(this.obj.value.replace(/(^\s*)|(\s*$)/g,'')==""){ return; }//值为空,退出
   try{ var reg = new RegExp("(" + this.obj.value + ")","i");}
   catch (e){ return; }
   var div_index=0;//记录创建的DIV的索引
   for(var i=0;i<valueArr.length;i++){
    if(reg.test(valueArr[i])){
     var div = document.createElement("div");
     div.className="auto_onmouseout";
     div.seq=valueArr[i];
     div.onclick=this.setValue(this);
     div.onmouseover=this.autoOnmouseover(this,div_index);
     div.innerHTML= result[i];
     console.log();
     this.autoObj.appendChild(div);
     this.autoObj.className="auto_show";
     div_index++;
    }
   }
  }
  this.pressKey(event);
  window.onresize=Bind(this,function(){this.init();});
 }
}
//-->

以上所述是小编给大家介绍的PHP自动补全表单的两种方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

PHP 相关文章推荐
php表单提交问题的解决方法
Apr 12 PHP
php长字符串定义方法
Jul 12 PHP
PHP 利用AJAX获取网页并输出的实现代码(Zjmainstay)
Aug 31 PHP
使用PHP遍历文件目录与清除目录中文件的实现详解
Jun 24 PHP
php文件缓存类用法实例分析
Apr 22 PHP
PHP实现将textarea的值根据回车换行拆分至数组
Jun 10 PHP
php可扩展的验证类实例(可对邮件、手机号、URL等验证)
Jul 09 PHP
php实现分页显示
Nov 03 PHP
PHP代码重构方法漫谈
Apr 17 PHP
PHP实现 APP端微信支付功能
Jun 22 PHP
laravel5环境隐藏index.php后缀(apache)的方法
Oct 12 PHP
PHP实现获取文件mime类型多种方法解析
May 28 PHP
PHP中for循环与foreach的区别
Mar 06 #PHP
微信公众平台开发-微信服务器IP接口实例(含源码)
Mar 05 #PHP
php实现留言板功能
Mar 05 #PHP
Linux服务器下PHPMailer发送邮件失败的问题解决
Mar 04 #PHP
php获取网站根目录物理路径的几种方法(推荐)
Mar 04 #PHP
PHP获取路径和目录的方法总结【必看篇】
Mar 04 #PHP
php redis实现对200w用户的即时推送
Mar 04 #PHP
You might like
通过PHP修改Linux或Unix口令的方法分享
2012/01/30 PHP
使用Discuz关键词服务器实现PHP中文分词
2014/03/11 PHP
ThinkPHP单字母函数(快捷方法)使用总结
2014/07/23 PHP
PHP实现抓取迅雷VIP账号的方法
2015/07/30 PHP
php is_executable判断给定文件名是否可执行实例
2016/09/26 PHP
在IIS下安装PHP扩展的方法(超简单)
2017/04/10 PHP
Google排名中的10个最著名的 JavaScript库
2010/04/27 Javascript
基于JQuery的访问WebService的代码(可访问Java[Xfire])
2010/11/19 Javascript
jQuery cdn使用介绍
2013/05/08 Javascript
js 获取radio按钮值的实例
2013/08/17 Javascript
JS实现闪动的title消息提醒效果
2014/06/20 Javascript
使用js Math.random()函数生成n到m间的随机数字
2014/10/09 Javascript
jQuery 遍历函数详解
2015/07/05 Javascript
JS实现网页游戏中滑块响应鼠标点击移动效果
2015/10/19 Javascript
浅析jquery与checkbox的checked属性的问题
2016/04/27 Javascript
改变checkbox默认选中状态及取值的实现代码
2016/05/26 Javascript
AngularJS 应用模块化的使用
2018/04/04 Javascript
JS实现电话号码的字母组合算法示例
2019/02/26 Javascript
关于vue-cli3打包代码后白屏的解决方案
2020/09/02 Javascript
Python logging管理不同级别log打印和存储实例
2018/01/19 Python
Python根据欧拉角求旋转矩阵的实例
2019/01/28 Python
python三引号输出方法
2019/02/27 Python
使用Django简单编写一个XSS平台的方法步骤
2019/03/25 Python
python设置环境变量的原因和方法
2019/06/24 Python
python 字符串常用方法汇总详解
2019/09/16 Python
numpy np.newaxis 的实用分享
2019/11/30 Python
flask框架自定义url转换器操作详解
2020/01/25 Python
python实现AHP算法的方法实例(层次分析法)
2020/09/09 Python
python 实现倒计时功能(gui界面)
2020/11/11 Python
HTML5之SVG 2D入门9—蒙板及mask元素介绍与应用
2013/01/30 HTML / CSS
HTML5 Canvas——用路径描画线条实例介绍
2013/06/09 HTML / CSS
澳大利亚墨水站Ink Station:墨水和碳粉打印机墨盒
2019/03/24 全球购物
应届生骨科医生求职信
2013/10/31 职场文书
认错检讨书
2014/10/02 职场文书
中秋节晚会开场白
2015/05/29 职场文书
openEuler 搭建java开发环境的详细过程
2022/06/10 Servers