THINKPHP+AJAX提交表单失败.

逸仙流 发布于 2014/05/07 16:06
阅读 2K+
收藏 0

public function in() {

		if ($_SESSION['verify'] !== md5($_POST['verify']) || !isset($_POST['verify'])) {
			$this->ajaxReturn('验证码有误!', 'msg', 0);
		}

		if (empty($_POST['username']) || empty($_POST['password'])) {
			$this->ajaxReturn('资料填写有误!', 'msg', 0);
		}

		$usersCls = D('User');

		if (!$usersCls->create($_POST)) {
			$this->ajaxReturn($usersCls->getError(), 'msg', 0);
		}

		//查找
		$result = $usersCls->Where('username="'.$_POST['username'].'" and password="'.md5($_POST['password']).'"')->Find();

		if(empty($result['username']) || empty($result['user_id'])){
			$this->ajaxReturn('没有此账户!', 'msg', 0);
		}

		//初始化
		$cookie = array();

		$cookie['UserName'] = $result['username'];
		$cookie['UserId'] 	= $result['user_id'];
		$cookie['LoginIp'] 	= NULL;

		$cookie = serialize($cookie);
		$this->time += 31536000;
		setcookie('User', $cookie, $this->time, '/');

		$this->redirect('Index/Index', '登陆成功,页面跳转中 ~');
		return FALSE;
	}

这是THINKPHP的代码

<!DOCTYPE html>
<html lang="en">
<head>
<title>用户登陆</title><meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="author" content="" />
<meta name="copyright" content="" />
<link rel="shortcut icon" type="image/x-icon" href="__ROOT__/favicon.jpg" />
<link rel="stylesheet" href="__PUBLIC__/Users/css/bootstrap.min.css" />
<link rel="stylesheet" href="__PUBLIC__/Users/css/bootstrap-responsive.min.css" />
<link rel="stylesheet" href="__PUBLIC__/Users/css/matrix-login.css" />
<link rel="stylesheet" href="__PUBLIC__/Users/font-awesome/css/font-awesome.css" />
<script src="__PUBLIC__/Users/js/jquery.min.js"></script>  
<script src="__PUBLIC__/Users/js/matrix.login.js"></script>
<script src="__PUBLIC__/Users/form/jquery.form.js"></script>
<script src="__PUBLIC__/Users/form/ajax.js"></script>
<script>
$(document).ready(function () {
	var options = {
		dataType:'json',
		type:'POST',
		url:'__URL__/in',
		target:'#Login',
		beforeSubmit:ajaxSubmit,
		success:ajaxSuccess,
		error:ajaxError
	}
		
	$('#Login').ajaxForm(options);
	
	$("#recoverform").hide();
});

function fleshVerify(){
    var time = new Date().getTime();
	document.getElementById("verifyImg").src = "__APP__/Public/Fun/verify/" + time;
}
</script>
</head>
<body>
<div id="loginbox">
	<div class="control-group normal_text"> 
		<h3><img src="__PUBLIC__/Users/img/logo.png" alt="Logo" /></h3>
	</div>
	
	<div id="loginform">
		<form name='Login' id="Login" class="form-vertical" action="#">
	    <div class="control-group">
	        <div class="controls">
	            <div class="main_input_box">
	                <span class="add-on bg_lg"><i class="icon-user"></i></span><input name='username' type="text" placeholder="邮箱或者手机号码." />
	            </div>
	        </div>
	    </div>
	    <div class="control-group">
	        <div class="controls">
	            <div class="main_input_box">
	                <span class="add-on bg_ly"><i class="icon-lock"></i></span><input name='password' type="password" placeholder="填写密码." />
	            </div>
	        </div>
	    </div>
	    <div class="control-group">
	        <div class="controls">
	            <div class="main_input_box">
	                <span class="add-on bg_ly"><i class="icon-lock"></i></span><input name='verify' type="text" placeholder="验证码." />
	                <img id="verifyImg" src="__URL__/verify" onClick="changeVerify()" title="点击刷新验证码"/>
	            </div>
	        </div>
	    </div>
		<div class="form-actions">
			<span class="pull-left"><a href="#" class="flip-link btn btn-info" id="to-recover">忘记密码?</a></span>
	   		<span class="pull-right"><input type='submit' value='登陆' class="btn btn-success" /></span>
		</div>
		{__TOKEN__}
		</form>
	</div>
	
	<div id="recoverform">
		<form id="setpassword" action="#" class="form-vertical">
		<p class="normal_text">输入您的电子邮件地址,我们将给你说明如何恢复密码。</p>
		
	    <div class="controls">
	        <div class="main_input_box">
	            <span class="add-on bg_lo"><i class="icon-envelope"></i></span><input type="text" placeholder="填写邮箱地址...." />
	        </div>
	    </div>
	   
	    <div class="form-actions">
	        <span class="pull-left"><a href="#" class="flip-link btn btn-success" id="to-login">&laquo; 返回登陆</a></span>
	        <span class="pull-right"><a class="btn btn-info">发送链接取回密码!</a></span>
	    </div>
		</form>
	</div>
	
	<div id='msg'></div>
</div>
</body>
</html>




这是HTML的代码

我想咨询一下~我的THINKPHP尝试去AJAX表单提交,按第一次会出现错误,就是AJAX提交表单失败,这是什么原因?按第二次会出现令牌表单错误,这样应该证明提交路径是对的,但为什么提交会失败呢?

加载中
0
逸仙流
逸仙流

求大神讲解,谢谢啊~~~~~~

0
dicvh0xq
dicvh0xq

提示错误是? 在php端打印POST看看 或者用firebug 看看请求包 

0
买红薯
买红薯
验证码和令牌用一次就失效了,当然会失败。
返回顶部
顶部