代码拉取完成,页面将自动刷新
<?php
include_once('config/config.php');
$LoginAdmin = checkAuth();
if ($_POST) {
$name = trim($_POST['name']) ?? '';
$mobile = trim($_POST['mobile']) ?? '';
$email = trim($_POST['email']) ?? '';
$address = trim($_POST['address']) ?? '';
$code = trim($_POST['code']) ?? '';
// 判断手机号是否重复
if ($mobile) {
$mobile_sql = "select `mobile` from `pre_person` where `mobile` = '$mobile'";
$mobile_data = find($mobile_sql);
if ($mobile_data) {
showMsg('手机号重复!','psAdd.php');
}
}
// 判断邮箱是否重复
if ($email) {
$email_sql = "select `email` from `pre_person` where `email` = '$email'";
$email_data = find($email_sql);
if ($email_data) {
showMsg('邮箱重复!','psAdd.php');
}
}
// 组装数据
$data = [
"name" => $name,
"mobile" => $mobile,
"email" => $email,
"sex" => $_POST['gender'],
"depid" => $_POST['depid'],
"jobid" => $_POST['jobid'],
"address" => $address,
"createtime" => time()
];
// 处理地区数据
$region_sql = "select `parentpath` from `pre_region` where `code` = '$code'";
$ParentPath = find($region_sql);
if (!$ParentPath) {
showMsg('所选地区不存在!','psAdd.php');
}
[$province, $city, $district] = explode(',', $ParentPath['parentpath']);
$data['province'] = $province;
$data['city'] = $city;
$data['district'] = $district;
// 处理头像数据
if ($_FILES['avatar']['tmp_name']) {
$avatar = build_upload('avatar');
if ($avatar['code'] === 0) {
@is_file('.' . $avatar['data']) && @unlink('.' . $avatar['data']);
showMsg($avatar['msg']);
}
$data['avatar'] = $avatar['data'];
}
// 插入数据
$result = add('person', $data);
if ($result['code'] == 0) {
showMsg($result['msg']);
} else {
showMsg($result['msg'], 'psList.php');
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php include_once('common/meta.php'); ?>
<link rel="stylesheet" href="/assets/lib/city-picker/city-picker.css">
</head>
<style>
input[type="file"] {
display: none;
}
label[for="avatar"] {
width: 200px;
}
#ts>span {
margin-left: unset;
}
</style>
<body>
<?php include_once('common/header.php'); ?>
<?php include_once('common/menu.php') ?>>
<div class="content">
<div class="header">
<h1 class="page-title">添加员工</h1>
</div>
<ul class="breadcrumb">
<li><a href="admin.php">admin</a> <span class="divider">/</span></li>
<li class="active">添加员工</li>
</ul>
<div class="container-fluid">
<div class="row-fluid">
<div class="well">
<div id="myTabContent" class="tab-content">
<div class="tab-pane active in" id="home">
<form method="post" enctype="multipart/form-data">
<label>姓名</label>
<input type="text" name="name" placeholder="请输入姓名" class="input-xxlarge" required>
<label>手机号</label>
<input type="text" name="mobile" placeholder="请输入手机号" class="input-xxlarge" required>
<label>邮箱</label>
<input type="email" name="email" placeholder="请输入邮箱" class="input-xxlarge" required>
<label>性别</label>
<select name="gender">
<option value="0">保密</option>
<option value="1">男</option>
<option value="2">女</option>
</select>
<label>部门</label>
<select name="depid" id="depid">
<option value="0">请选择</option>
</select>
<label>职位</label>
<select name="jobid" id="jobid">
<option value="0">请选择</option>
</select>
<label>选择地区</label>
<div style="position: relative; height: 30px; margin-bottom: 10px;" id="ts">
<input type="text" data-toggle="city-picker" id="region" class="input-xxlarge">
<input type="hidden" name="code" id="code">
</div>
<label>详细地址</label>
<input type="text" name="address" placeholder="请输入详细地址" class="input-xxlarge" required>
<label>头像</label>
<label for="avatar">
<img src="/assets/images/upload.png" id="upload" alt="">
</label>
<input type="file" name="avatar" id="avatar" class="input-xlarge">
<label></label>
<input class="btn btn-primary" type="submit" value="提交" />
</form>
</div>
</div>
</div>
<?php include_once('common/footer.php'); ?>
</div>
</div>
</div>
</body>
</html>
<?php include_once('common/script.php'); ?>
<script src="/assets/lib/city-picker/city-picker.data.min.js"></script>
<script src="/assets/lib/city-picker/city-picker.min.js"></script>
<script>
// 图片预览
$('#avatar').change(function() {
let file = $(this)[0].files[0];
if (file) {
let reader = new FileReader();
reader.readAsDataURL(file)
// 事件
reader.onload = function() {
$('#upload').attr('src', reader.result);
}
}
});
// 立即执行函数 --> 发送ajax请求部分数据
(function() {
$.ajax({
type: 'post',
url: 'api.php',
data: {
action: 'dep',
adminid: <?php echo $LoginAdmin['id'] ?>
},
dataType: 'json',
success: res => {
if (res.code === 0) {
console.log(res.msg);
} else {
let option = '';
for (let item of res.data) {
option += `<option value="${item.id}">${item.name}</option>`;
}
$('#depid').append(option);
}
}
})
})();
// 根据部门接收职位数据
$('#depid').change(function() {
let depid = $('#depid').val() ?? 0;
$.ajax({
type: 'post',
url: 'api.php',
data: {
action: 'job',
depid,
adminid: <?php echo $LoginAdmin['id'] ?>
},
dataType: 'json',
success: res => {
if (res.code == 0) {
console.log(res.msg);
$('#jobid').html(`<option value="0">请选择</option>`);
} else {
let option = '';
for (let item of res.data) {
option += `<option value="${item.id}">${item.name}</option>`;
}
$('#jobid').html(option);
}
}
})
})
// 选择地区
$("#region").on("cp:updated", function() {
var citypicker = $(this).data("citypicker");
var code = citypicker.getCode("district") || citypicker.getCode("city") || citypicker.getCode("province");
$("#code").val(code);
});
</script>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。