当前位置:首页 > PHP教程 > PHP常见问题

thinkphp3.2实现在线留言提交验证码功能

如何实现验证码成功验证功能?提交验证码?这篇文章为大家解答。

把今天掌握的东西整理一下,要不然,我就忘干净了:

今天在做一个企业网站的时候,有一个在线留言的功能,最后提交的时候需要输入验证码。如图下:

当然,特连接的并不是我的后台

好了,开始了,首先我需要把验证码显示出来,前端页面:

<div class="message-con clearfix"> <div class="mcon-left pic pull-left"><img src="/public/admin/xx/images/message.png" class="vcenter" alt="" /></div> <div class="mcon-right pull-right"> <form method="post" action="{:u('validate')}" name='valiate'> <label><span>姓名</span><input type="text" name="xingming" class="mname" value=""/><em>*</em></label> <label><span>联系电话</span><input type="text" name="tel" class="mtel" /><em>*</em></label> <label><span>联系地址</span><input type="text" name="dizhi" class="madd" /></label> <label><span>邮箱地址</span><input type="text" name="youxiang" class="memail" /></label> <label><span>留言内容</span><textarea name="content" class="mcontent"></textarea><em>*</em></label> <label class="mcodela"><input type="text" name="code" class="mcode" /> <img src="{:u('verify')}" onclick="this.src=this.src+'?'+math.random();" alt="" /> <a href="#" rel="external nofollow" >看不清楚?换一张</a><em>*</em></label> <label><input type="submit" class="msub" name="tj" value="在线提交" /></label> </form> </div> </div> </div>

好 点击事件,name,提交按钮都改好了,走起,去控制器啦;

来到控制器这里,先做一件事,引入model吧:

use otcmsmodel;

啥?没有model类  自己去写吧:

<?php namespace otcmsmodel; use thinkmodel; class usermodel extends model{ protected $_validate = array( array('xingming','require','姓名不可空!'), array('tel','require ','手机号不可空!'), array('dizhi','require','地址不可空!'), array('youxiang',' require','邮箱不可空!'), array('content','require','内容不可空!'), ); }

设置验证码:

显示:

public function verify(){ $config = array( 'fontsize' => 30, // 验证码字体大小 'length' => 4, // 验证码位数 ); $verify = newthinkverify($config); $verify->entry(); }

下面来写条件:

public function validate(){ $date['xingming'] = i('post.xingming'); $date['tel'] = i('post.tel'); $date['dizhi'] = i('post.izhi'); $date['youxiang'] = i('post.youxiang'); $date['content'] = i('post.content'); $yzm = i('post.code'); $fkyz = d("liuyan"); if (!$fkyz->create()){ // 如果创建失败 表示验证没有通过 输出错误提示信息 exit($fkyz->geterror()); }else{ // 验证通过 可以进行其他数据操作 $verify = new thinkverify(); $yzmyz = $verify->check($yzm); if(!$yzmyz){ $this->error('验证码错误'); } else{ $validate = m("liuyan"); $validate->add($date); $this->success('添加成功'); } } }

图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持硕编程。



【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐