本次主要就是两个文件,一个是控制器文件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();
}
如何查看是否有放回值呢,