和asp.net类似,一个FormAction对应Form文件夹
demo结构:
‘
对于项目结构有疑问的: http://www.thinkphp.cn/document/60.html
login.html
<!DOCTYPE html> <html lang="en"> <head></head> <body> <meta charset="UTF-8"> <FORM method="post" action="__URL__/logFind">//注意action 用户名:<INPUT type="text" name="username"><br/> 密码:<INPUT type="password" name="password"><br/> <INPUT type="submit" value="登录"> </FORM> </body> </html>
regist.html
<!DOCTYPE html> <html lang="en"> <head></head> <body> <meta charset="UTF-8"> <FORM method="post" action="__URL__/RegInsert"> 用户名:<INPUT type="text" name="username"><br/> 密码:<INPUT type="password" name="password"><br/> <INPUT type="submit" value="注册"> </FORM> </body> </html>
FormAction.class.php
class FormAction extends Action{ public function logFind(){ $Form = D('Form'); if($Form->create()) { $username=$this->_post("username"); $password=$this->_post("password"); $User = M("User"); $map['username&password'] =array($username,$password,'_multi'=>true);//多条件查找一定要加上_multi=>true $data=$User->where($map)->find(); if($data) { echo "登录成功"; } else { $user['username']=$username; $pasw['password']=$password; $data=$User->where($user)->find();//用username查找该用户的所有信息 if($data==null) { redirect(U("Form/regist"),1,"用户名不存在,即将跳转注册"); } else { redirect(U("Form/login"),1,"密码出错,跳转登录"); } } } } public function RegInsert() { $user = D('user');//user->add()时才会添加到user那张表,如果是$form=D('form'),就会添加到form这张表 if($user->create()) { $result = $user->add();//判断插入是否成功 if($result) { $this->success('操作成功!'); }else{ $this->error('写入错误!'); } }else{ $this->error($Form->getError()); } } }