//html页面
</pre><pre name="code" class="html">
<pre name="code" class="html"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<title>新建网页</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form action="{:U('Home/Index/add')}" id="form" method="post" enctype="multipart/form-data">
<p>
<label for="">用户名:</label>
<input type="text" name="name" id="name">
</p>
<p>
<label for="">性别:</label>
<input type="radio" name="sex" checked="checked" οnclick="current($(this))" data-value="1"/>男
<input type="radio" name="sex" οnclick="current($(this))" data-value="2"/>女
<input type="hidden" name="sex" id="sex" value="1"/>
</p>
<p>
<label for="">联系电话:</label>
<input type="text" name="mobile" id="mobile">
</p>
<p>
<input type="file" value="图片" name="image"/>
</p>
</form>
<!-- <a href="javascript:void(0);" data-url="{:U('Home/Index/add')}" id="btn">提交</a> -->
<a href="javascript:void(0);" id="btn">提交</a>
<script src="/Public/Home/js/jquery-1.8.0.min.js"></script>
<script>
function current(obj){
var a = obj.data('value');
obj.addClass('current').siblings().removeClass('current');
console.log(obj.siblings('input').val());
obj.siblings('input').val(a);
}
$('#btn').click(function(){
var name =$.trim($("#name").val());
var mobile =$.trim($("#mobile").val());
var sex = $.trim($("#sex").val());
if(name == ""){
alert("姓名不能为空");
}else if(mobile ==""){
alert("联系电话不能为空");
}else {
//ajax提交
// $.ajax({
// type:'post',
// url:$('#btn').data('url'),
// data:{name:name,sex:sex,mobile:mobile},
// success:function(data) {
// if (data.status == 1) {
// alert(data.info);
// } else {
// alert(data.info);
// }
// }
// });
$("#form").submit();
}
});
</script>
</body>
</html>
//php 代码
public function add(){
if (IS_POST) {
$UModel = D('User');
$data['name'] = $_POST['name'];
$data['mobile'] = $_POST['mobile'];
$data['sex']= $_POST['sex'];
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 3145728 ;// 设置附件上传大小
$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->rootPath = './Public/Home/upload/'; // 设置附件上传根目录
$upload->savePath = ''; // 设置附件上传(子)目录
// 上传文件
$info = $upload->upload();
var_dump($info);
// if(!$info) {// 上传错误提示错误信息
// $this->error($upload->getError());
// }else{// 上传成功
// $this->success('上传成功!');
// }
$data['image'] =$info['image']['name'];
// $data['sex']= I('post.sex');
$UModel->create();
$UModel ->add($data);
}
$this->display();
}