先上demo
PHP复制代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns ="http://www.w3.org/1999/xhtml">
<head >
<meta http -equiv ="Content-Type" content ="text/html; charset=utf-8" />
<title >NASDAO </title >
<script src ="js/jquery-1.8.0.min.js" type ="text/javascript" ></script>
<script type ="text/javascript" src ="js/login.js"></script>
<link rel ="stylesheet" type ="text/css" href ="css/register.css"/>
</head >
<body >
<div class='signup_container'>
<h1 class='signup_title'>登录系统 </h1 >
<img src ='images/people.png' id ='admin'/>
<div id ="signup_forms" class="signup_forms clearfix">
<form class="signup_form_form" id ="signup_form" method ="post" action ="" data -secure -action ="https://www.tumblr.com/login" data -secure -ajax -action ="">
<div class="form_row first_row">
<label for="signup_email">请输入用户名 </label ><div class='tip ok'></div >
<input type ="text" placeholder ="请输入用户名" id ="signup_name" data -required ="required">
</div >
<div class="form_row">
<label for="signup_password">请输入密码 </label ><div class='tip ok'></div >
<input type ="password" placeholder ="请输入密码" id ="signup_password" data -required ="required">
</div >
<div class="form_row">
<input type ="text" name ="user[password]" placeholder ="" id ="signup_select" value ='' data -required ="required">
<img src ='images/d.png' id ='d'/>
<ul >
<li >管理员 </li >
</ul >
</div >
</form >
</div >
<div class="login-btn-set"><div class='rem'>记住我 </div > <a id ="login" class='login-btn'></a ></div >
<p class='copyright'>版权所有 </p >
</div >
</body >
<script type ="text/javascript">
$ (function(){
$ ('.rem').click (function(){
$ (this ).toggleClass ('selected');
})
$ ('#signup_select').click (function(){
$ ('.form_row ul').show ();
event .cancelBubble = true;
})
$ ('#d').click (function(){
$ ('.form_row ul').toggle ();
event .cancelBubble = true;
})
$ ('body').click (function(){
$ ('.form_row ul').hide ();
})
$ ('.form_row li').click (function(){
var v = $ (this ).text ();
$ ('#signup_select').val (v );
$ ('.form_row ul').hide ();
})
})
</script>
</html > 复制代码
视图里不包含任何php代码是用jquery提交给页面
JS复制代码 $(document).ready(function(){$("a#login").bind("click",enter);
function enter(){
name = $("#signup_name").val();
password = $("#signup_password").val();
param = {"name":name,"password":password};
$.ajax({
contentType:"application/x-www-form-urlencoded; charset=utf-8",
type:'post',
url:'index.php/admin/query', //提交给指定的方法
data:param,
async:false,
success: function(msg){
if(msg=="001"){
location.href ='home.php';
}else{
alert(msg);
//document.forms[0].action='http://www.1.cn';
}
}
});
};
});
复制代码
控制器
PHP复制代码 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class admin extends CI_Controller {
// 装载父类控制器
function __construct ()
{
parent ::__construct ();
//$this->load->helper('url'); //已经被ci自动加载
//$this->load->library('session'); //已经被ci自动加载
}
public function index ()
{
$this->load->helper('form');
$this->load->view('login');
}
public function query ()
{
$this->load->model('Mlogin');
$query = $this->Mlogin->Select();
if($query)
{
$data = array(
'name' => $this->input->post('name'),
'is_logged_in' => true
);
//$ssoname = $this->input->post('name');
$this->session->set_userdata($data);
echo '22';
//echo $ssoname;
}
else
{
$this->index();
echo 'error';
}
}
} 复制代码
模型------
PHP复制代码 <?php
class Mlogin extends CI_Model {
function __construct (){
parent ::__construct ();
}
function Select ()
{
//$this->load->database();
//$this->db->where('status=','5');
//$this->db->select('name');
//$query = $this->db->get('loginlog');
//$sql="select name,password from nasdaouser where 1=1 and loginstatus=0";
//$log=$this->db->query($sql,array(5));
//return $log->result();
$this->db->where('name',$this->input->post('name'));
$this->db->where('password',md5(base64_encode($this->input->post('password'))));
$query = $this->db->get('nasdaouser');
if($query->num_rows() == 1)
{
return true;
}
}
}
?> 复制代码
视图没有代码,是纯由jquery AJAX提交给指定的控制器
在点击登录后,检查session 看见已经写入,然后就出现500错误
|