jquery ajax的验证实例
2015-07-22 12:20
579 查看
首先需要在项目中创建文件夹js,导入jquery.js,文件在我的资源可免费下载
form表单中的submit:
<input type="submit" value="登录" onclick="return test();"/>
head标签中:
<head>
<script type="text/javascript" src="./js/jquery.js"></script>
<script type="text/javascript">
function test(){
var username = $('#username').val();
var aa = false;
$.ajax({
type: 'get',
//异步请求
async:false,
//url的样式(url: 'http://localhost/工程名/index.php/Home/控制器的名字/方法名' ,),下面是我自己的例子
url: 'http://localhost/ad/index.php/Home/User/ispeople' ,
data: {
username:username
} ,
success: function(data){
if(data.msg==true) {
alert("此用户名正确!");
aa = true;
} else {
alert("无此用户名,请重新输入!");
aa = false;
}
} ,
dataType: 'json'
});
return aa;
}
</script>
</head>
控制器里面的方法:
//判断用户是否存在
public function ispeople(){
$username=$_GET['username'];
$user = M('User');
$condition['username'] = $username;
$list = $user->where($condition)->find();
if($list){
$this->ajaxReturn(array('msg'=>TRUE));
} else {
$this->ajaxReturn(array('msg'=>FALSE));
}
}
form表单中的submit:
<input type="submit" value="登录" onclick="return test();"/>
head标签中:
<head>
<script type="text/javascript" src="./js/jquery.js"></script>
<script type="text/javascript">
function test(){
var username = $('#username').val();
var aa = false;
$.ajax({
type: 'get',
//异步请求
async:false,
//url的样式(url: 'http://localhost/工程名/index.php/Home/控制器的名字/方法名' ,),下面是我自己的例子
url: 'http://localhost/ad/index.php/Home/User/ispeople' ,
data: {
username:username
} ,
success: function(data){
if(data.msg==true) {
alert("此用户名正确!");
aa = true;
} else {
alert("无此用户名,请重新输入!");
aa = false;
}
} ,
dataType: 'json'
});
return aa;
}
</script>
</head>
控制器里面的方法:
//判断用户是否存在
public function ispeople(){
$username=$_GET['username'];
$user = M('User');
$condition['username'] = $username;
$list = $user->where($condition)->find();
if($list){
$this->ajaxReturn(array('msg'=>TRUE));
} else {
$this->ajaxReturn(array('msg'=>FALSE));
}
}
相关文章推荐
- jquery语法
- 浅析jQuery对select操作小结(遍历option,操作option)
- 【JQuery】jQuery.inArray 确定第一个参数在数组中的位置
- PHP结合jQuery实现找回密码
- Extjs和jQuery的区别
- js(jquery)绑定点击事件
- jquery+validate+blockUI小例子
- jquery实现页面加载时删除特定class 的div内前三个字符
- jquery.attr 等类似的多源函数问题
- PHP结合jQuery实现的评论顶、踩功能
- jquery ajax如何传多个值到后台页面
- jQuery-图片的放大镜显示效果(不需要大小图) ,放大镜图层显示在图片左右侧,不适用table
- js,jQuery数组常用操作小结
- 使用jQuery图表插件Sparklines来开发一个实用的网站PV(page view)实时监控应用
- 学习笔记:jquery1.9版本后废弃的函数和方法
- jquery掷筛子动画 整理
- jQuery掷色子动画
- jQuery常用选择器汇总
- jQuery原型属性和方法总结
- jQuery ui datepicker 日历转中文