//模版实现
<!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/index')}" id="form" method="post">
<p>
<label for="">用户名:</label>
<input type="text" name="name" id="name">
</p>
<p>
<label for="">联系电话:</label>
<input type="text" name="amobile" id="mobile">
</p>
</form>
<a href="javascript:void(0);" data-url="{:U('Home/Index/index')}" id="btn">提交</a>
<script src="/Public/Home/js/jquery-1.8.0.min.js"></script>
<script>
$('#btn').click(function(){
var name =$.trim($("#name").val());
var mobile =$.trim($("#mobile").val());
if(name == ""){
alert("姓名不能为空");
}else if(mobile ==""){
alert("联系电话不能为空");
}else {
$.post($('#btn').data('url'),{name:name,mobile:mobile},function(data){
if (data.status == 1) {
alert(data.info);
} else {
alert(data.info);
}
});
}
});
</script>
</body>
</html>
//后台方法
public function index(){
if (IS_POST) {
$UModel = D('User');
$data['name'] = $_POST['name'];
$data['mobile'] = $_POST['mobile'];
$UModel->create();
if($UModel ->add($data)){
$this->success("成功",'home/index/index');
}else{
$this->error("失败",'home/index/index');
}
}
$this->display();
}
$.post 提交数据
最新推荐文章于 2024-03-14 15:45:57 发布