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面向对象全攻略 (八)重载新的方法
Sep 30 PHP
PHP写的获取各搜索蜘蛛爬行记录代码
Aug 21 PHP
探讨PHP中this,self,parent的区别详解
Jun 08 PHP
为百度UE编辑器上传图片添加水印功能
Apr 16 PHP
PHP SPL标准库中的常用函数介绍
May 11 PHP
php中smarty区域循环的方法
Jun 11 PHP
php生成数字字母的验证码图片
Jul 14 PHP
如何批量清理系统临时文件(语言:C#、 C/C++、 php 、python 、java )
Feb 01 PHP
PHP读取大文件的多种方法介绍
Apr 04 PHP
PHP微信公众号自动发送红包API
Jun 01 PHP
php curl简单采集图片生成base64编码(并附curl函数参数说明)
Feb 15 PHP
php变量与字符串的增删改查操作示例
May 07 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中文件缓存转内存缓存的方法
2011/12/06 PHP
浅析php变量修饰符static的使用
2013/06/28 PHP
php微信开发接入
2016/08/27 PHP
PHP+MariaDB数据库操作基本技巧备忘总结
2018/05/21 PHP
Centos7.7 64位利用本地完整安装包安装lnmp/lamp套件教程
2021/03/09 Servers
jquery 实现表单验证功能代码(简洁)
2012/07/03 Javascript
js类定义函数时用prototype与不用的区别示例介绍
2014/06/10 Javascript
JavaScript设计模式之单例模式实例
2014/09/24 Javascript
bootstrap table 服务器端分页例子分享
2015/02/10 Javascript
js实现在网页上简单显示时间的方法
2015/03/02 Javascript
js时间控件只显示年月
2017/01/08 Javascript
快速处理vue渲染前的显示问题
2018/03/05 Javascript
JS常见DOM节点操作示例【创建 ,插入,删除,复制,查找】
2018/05/14 Javascript
vue-cli webpack 引入swiper的操作方法
2018/09/15 Javascript
JavaScript数据结构与算法之二叉树插入节点、生成二叉树示例
2019/02/21 Javascript
Python yield 小结和实例
2014/04/25 Python
详解Python Socket网络编程
2016/01/05 Python
Python科学计算之Pandas详解
2017/01/15 Python
python 编程之twisted详解及简单实例
2017/01/28 Python
Python进阶学习之特殊方法实例详析
2017/12/01 Python
解决pyinstaller打包exe文件出现命令窗口一闪而过的问题
2018/10/31 Python
在Pycharm terminal中字体大小设置的方法
2019/01/16 Python
python中logging模块的一些简单用法的使用
2019/02/22 Python
详解python的super()的作用和原理
2020/10/29 Python
pandas 数据类型转换的实现
2020/12/29 Python
美国知名平价彩妆品牌:e.l.f. Cosmetics
2017/11/20 全球购物
WoolOvers澳洲官方网站:英国针织服装公司
2018/05/13 全球购物
理肤泉俄罗斯官网:La Roche-Posay俄罗斯
2018/07/24 全球购物
Chupi官网:在爱尔兰手工制作的订婚、结婚戒指和精美珠宝
2020/09/28 全球购物
应届生高等护理求职信
2013/10/12 职场文书
优秀学生自我鉴定范例
2013/12/18 职场文书
社区工作者先进事迹
2014/01/18 职场文书
学校献爱心活动总结
2014/07/08 职场文书
Python基础之常用库常用方法整理
2021/04/30 Python
MySQL Shell import_table数据导入的实现
2021/08/07 MySQL
Redis如何实现分布式锁
2021/08/23 Redis