thinkphp-ajax异步验证之ajaxReturn的使用

本次主要就是两个文件,一个是控制器文件home的IndexAction.class.php以及home的模板文件index.html

index.html

<div class="form">
				<form id='form' onsubmit="return false" action="" method="post" enctype="multipart/form-data">
					<select name="type_id" class="zb"> 
						<option value="">组别</option>
						<foreach name = "type" item = "v">
						<option value="{$v.id}" name="type_id">{$v.name}</option>
						</foreach>
					</select>

					<select name="user_id" class="xm"> 
						<option value="">姓名</option>
					</select>
					<button type="submit" class="dk" onclick="dk()">打卡</button>
				</form>
			</div>

index.html的ajax异步代码

<script type="text/javascript">
$('.zb').change(function(){
    var typeid = $(this).val();
    var url = '/index.php/index/index';
    postDate = {
        'id':typeid
    };
    $.post(url,postDate,function(result){
    	html = '<option value="">姓名</option>';
   		for (var i = 0; i <result.data.length; i++) {
   			 name = result.data[i].name;
   			 user_id = result.data[i].id;
   			 html +='<option value="'+user_id+'" name="user_id">'+name+'</option>';
   		}
   		$('.xm').html(html);
    },"json");
});

$('.dk').click(function(){
	if($('.zb').val() == ''){
		alert('组别不能为空');
	}else if($('.xm').val() == ''){
		alert('姓名不能为空');
	}
	var url = 'index.php/index/add';
	postDate = $('#form').serialize();
	$.post(url,postDate,function(result){
		if(result.info == '-1'){
			alert('你已经打卡,请勿重复');
		}else{
			$('.dkxm').html(result.data.name);
			$('.dkxm').parents('.tck').animate({"bottom": '0'},300);
		}
    },"json");
});

$('.jt').click(function(){
	$(this).parents('.tck').animate({"bottom": '-200px'},300);
});
</script>

接着在控制器里对应的方法

public function add(){
        if(IS_POST){
            if($_POST['type_id']==""){
                $this->error('组别不能为空');
            }
            if($_POST['user_id']==""){
                $this->error('姓名不能为空');
            }
            $time = time();
            $userid = $_POST['user_id'];
            $start_stime  = strtotime(date('Y-m-d 0:0:0',$time))-1;
            $end_stime = strtotime(date('Y-m-d 23:59:59',$time))+1;
            $info = D('dk')->where("user_id = $userid and time > $start_stime and time < $end_stime")->find();
            $user = D('user')->field('name')->where('id='.$userid)->find();
            if($info){
                $this->ajaxReturn($info,'-1',1);
                exit;
            }
            $dk=D('dk');
            $dk->create();  
            $dk->time=time();  
            $z=$dk->add();
            if($z){
                $this->ajaxReturn($user,'打卡成功',1);
                exit;
            }else{
                $this->error('打卡失败');
            }
        }
       $this->display();
    }

如何查看是否有放回值呢,
获取到选择组别时返回的组员名称

选择打卡时返回正确数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值