php ajax或jquery 表单中 鼠标失去焦点 自动无刷新存入mysql 5
1个回答
展开全部
【给你个一个ajax的例子,你看看 希望能帮助你】
第一步需要一个 jq的js文件
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
<script type="text/javascript" src="<?php echo $this->baseUrl ?>/public/js/jq1.7.js"></script>
<script language="javascript">
$(document).ready(function(){
$("#submit").click(function(){
if( $("#uname").val() == "" || $("#upas").val() == "" ){
alert("用户名或者密码不能为空");
return false;
}else{
$("#showerrormessage").load(
'<?php echo $this->baseUrl ?>/index/login',
{uname:$("#uname").val(),upas:$("#upas").val()},
function(data){
if(data == 1){
$("#showerrormessage").html('login success!!!');
}else{
$("#showerrormessage").html('login error~~~');
}
}
);
}
});
});
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
用户名:<input type="text" name="uname" id="uname" /><br /><br />
密 码:<input type="password" name="upas" id="upas" /><br /><br />
<input type="submit" id="submit" value=" .. 登陆 .. " style="margin-left:60px;" />
</form>
<div id="showerrormessage" style="color:#fd0;font-size:18px;"></div>
</body>
</html>
这是页面文件
下面是 php文件 一个方法
/**
* 登陆方法
* **/
function loginAction(){
if(strtolower($_SERVER['REQUEST_METHOD']) == 'post'){
//$name = $this->_request->getpost("uname");
$name = $this->getRequest()-> getQuery("uname");
//$pws = $this->_request->getpost("upas");
$pws = $this->getRequest()-> getQuery("upas");
$userTable = new Username();
$where = "name = '".$name."' and paws = '".$pws."' ";
$result_all = $userTable -> fetchAll($where) -> toArray();
if(count($result_all) > 0){
$this->_helper->getHelper('Json')-> sendJson(1);
$SetSession = new Zend_Session_Namespace('setusersession');//存储session
$SetSession -> sname = $name;
$SetSession -> spws = $pws;
echo $this -> view -> render('bluser.phtml');
}else{
$this->_helper->getHelper('Json')-> sendJson(2);
}
}else{
echo $this -> view -> render('login.phtml');
}
}
第一步需要一个 jq的js文件
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
<script type="text/javascript" src="<?php echo $this->baseUrl ?>/public/js/jq1.7.js"></script>
<script language="javascript">
$(document).ready(function(){
$("#submit").click(function(){
if( $("#uname").val() == "" || $("#upas").val() == "" ){
alert("用户名或者密码不能为空");
return false;
}else{
$("#showerrormessage").load(
'<?php echo $this->baseUrl ?>/index/login',
{uname:$("#uname").val(),upas:$("#upas").val()},
function(data){
if(data == 1){
$("#showerrormessage").html('login success!!!');
}else{
$("#showerrormessage").html('login error~~~');
}
}
);
}
});
});
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
用户名:<input type="text" name="uname" id="uname" /><br /><br />
密 码:<input type="password" name="upas" id="upas" /><br /><br />
<input type="submit" id="submit" value=" .. 登陆 .. " style="margin-left:60px;" />
</form>
<div id="showerrormessage" style="color:#fd0;font-size:18px;"></div>
</body>
</html>
这是页面文件
下面是 php文件 一个方法
/**
* 登陆方法
* **/
function loginAction(){
if(strtolower($_SERVER['REQUEST_METHOD']) == 'post'){
//$name = $this->_request->getpost("uname");
$name = $this->getRequest()-> getQuery("uname");
//$pws = $this->_request->getpost("upas");
$pws = $this->getRequest()-> getQuery("upas");
$userTable = new Username();
$where = "name = '".$name."' and paws = '".$pws."' ";
$result_all = $userTable -> fetchAll($where) -> toArray();
if(count($result_all) > 0){
$this->_helper->getHelper('Json')-> sendJson(1);
$SetSession = new Zend_Session_Namespace('setusersession');//存储session
$SetSession -> sname = $name;
$SetSession -> spws = $pws;
echo $this -> view -> render('bluser.phtml');
}else{
$this->_helper->getHelper('Json')-> sendJson(2);
}
}else{
echo $this -> view -> render('login.phtml');
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询