批改状态:合格
老师批语:
员工管理系统的系统设置功能的实现——
系统设置界面的代码——
<?php
if (isset($_SESSION['username'])):
?>
<script>
location.href = "login.php";
</script>
<?php else: ?>
<?php
$pdo = new PDO('mysql:host=127.0.0.1;dbname=php', 'root', 'root');
if (isset($_GET['action']) && $_GET['action'] == 'save') {
// print_r($_POST);
$sql = "UPDATE `system` SET `name` = :name,`address` = :address,`tel` = :tel,`closed` = :closed WHERE `id` = :id";
$stmt = $pdo->prepare($sql);
$name = $_POST['name'];
$id = $_POST['id'];
$address = $_POST['address'];
$closed = $_POST['closed'];
$tel = $_POST['tel'];
$stmt->bindParam('name', $name, PDO::PARAM_STR);
$stmt->bindParam('tel', $tel, PDO::PARAM_STR);
$stmt->bindParam('address', $address, PDO::PARAM_STR);
$stmt->bindParam('closed', $closed, PDO::PARAM_INT);
$stmt->bindParam('id', $id, PDO::PARAM_INT);
if ($stmt->execute()) {
if ($stmt->rowCount() == 1) {
$status = 1;
$message = '数据更新成功';
} elseif ($stmt->rowCount() == 0) {
$status = 0;
$message = '数据未更新';
}
} else {
$status = -1;
$message = '数据更新失败';
}
echo json_encode(['status' => $status, 'message' => $message]);
die;
}
$sql = "SELECT * FROM `system` LIMIT 1;";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$system = $stmt->fetch(PDO::FETCH_ASSOC);
?>
<link rel="stylesheet" href="layui\css\layui.css">
<script src="layui\layui.js"></script>
<html>
<head></head>
<style>
.success {
color: #5FB878;
}
.error {
color: #FF5722;
}
.layui-col-md4 {
width: 33%;
}
.a {
width: 33%;
margin: 0 auto;
/*float: left;*/
}
.layui-form-label {
width: 60px;
}
</style>
<body style="overflow:hidden;">
<meta charset="UTF-8">
<div class="layui-row layui-col-space1">
<div class="a">
<form action="" class="layui-form">
<h1 style="padding-left:90px;">系统设置</h1>
<br>
<div class="layui-form-item">
<label class="layui-form-label">公司姓名</label>
<div class="layui-input-inline">
<input type="text" required lay-verify="required" autocomplete="off"
class="layui-input" name="name" value="<?= $system['name'] ?>">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">公司地址</label>
<div class="layui-input-inline">
<input type="text" required lay-verify="required" autocomplete="off"
class="layui-input" name="address" value="<?= $system['address'] ?>">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">公司电话</label>
<div class="layui-input-inline">
<input type="text" required lay-verify="required" autocomplete="off"
class="layui-input" name="tel" value="<?= $system['tel'] ?>">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">站点控制</label>
<div class="layui-input-inline">
<?php if ($system['closed'] == 1): ?>
<input type="radio" name="closed" value="0" title="开启">
<input type="radio" name="closed" value="1" title="关闭" checked>
<?php else: ?>
<input type="radio" name="closed" value="0" title="开启" checked>
<input type="radio" name="closed" value="1" title="关闭">
<?php endif; ?>
</div>
</div>
<button class="layui-btn" style="margin-left: 90px;" onclick="save(this.form);" type="button">保存
</button>
<button class="layui-btn" onclick="history.back();" type="button">退出</button>
<input type="hidden" value="<?php echo $system['id']; ?>" name="id">
<p style="padding-left: 90px;"></p>
</form>
</div>
</div>
</body>
</html>
<?php endif; ?>
<script>
layui.use('form', function () {
var form = layui.form;
})
function save(form) {
var tips = form.lastElementChild;
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
// tips.innerHTML = 'success';
var data = JSON.parse(request.responseText);
console.log(request.responseText);
switch (data.status) {
case 1:
tips.classList.add('success');
tips.classList.remove('error');
break;
case 0:
tips.classList.add('error');
tips.classList.remove('success');
break;
case -1:
tips.classList.add('error');
tips.classList.remove('success');
break;
}
tips.innerHTML = data.message;
}
}
request.open('POST', '<?=$_SERVER['PHP_SELF']?>?action=save');
request.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
request.send('name=' + form.name.value + '&address=' + form.address.value + '&id=' + form.id.value + '&tel=' + form.tel.value + '&closed=' + form.closed.value);
}
</script>注:——$_SERVER['PHP_SELF']——当前正在执行脚本的文件名,与 document root相关。实现在同一页面中使用ajax。
$_SERVER[](服务器变量)——常用的几种
$_SERVER['PHP_SELF'] #当前正在执行脚本的文件名,与 document root相关。 $_SERVER['argv'] #传递给该脚本的参数。 $_SERVER['argc'] #包含传递给程序的命令行参数的个数(如果运行在命令行模式)。 $_SERVER['GATEWAY_INTERFACE'] #服务器使用的 CGI 规范的版本。例如,“CGI/1.1”。 $_SERVER['SERVER_NAME'] #当前运行脚本所在服务器主机的名称。 $_SERVER['SERVER_SOFTWARE'] #服务器标识的字串,在响应请求时的头部中给出。 $_SERVER['SERVER_PROTOCOL'] #请求页面时通信协议的名称和版本。例如,“HTTP/1.0”。 $_SERVER['REQUEST_METHOD'] #访问页面时的请求方法。例如:“GET”、“HEAD”,“POST”,“PUT”。 $_SERVER['QUERY_STRING'] #查询(query)的字符串。 $_SERVER['DOCUMENT_ROOT'] #当前运行脚本所在的文档根目录。在服务器配置文件中定义。 $_SERVER['HTTP_ACCEPT'] #当前请求的 Accept: 头部的内容。 $_SERVER['HTTP_ACCEPT_CHARSET'] #当前请求的 Accept-Charset: 头部的内容。例如:“iso-8859-1,*,utf-8”。 $_SERVER['HTTP_ACCEPT_ENCODING'] #当前请求的 Accept-Encoding: 头部的内容。例如:“gzip”。 $_SERVER['HTTP_ACCEPT_LANGUAGE']#当前请求的 Accept-Language: 头部的内容。例如:“en”。 $_SERVER['HTTP_CONNECTION'] #当前请求的 Connection: 头部的内容。例如:“Keep-Alive”。 $_SERVER['HTTP_HOST'] #当前请求的 Host: 头部的内容。 $_SERVER['HTTP_REFERER'] #链接到当前页面的前一页面的 URL 地址。 $_SERVER['HTTP_USER_AGENT'] #当前请求的 User-Agent: 头部的内容。 $_SERVER['HTTPS'] — 如果通过https访问,则被设为一个非空的值(on),否则返回off $_SERVER['REMOTE_ADDR'] #正在浏览当前页面用户的 IP 地址。 $_SERVER['REMOTE_HOST'] #正在浏览当前页面用户的主机名。 $_SERVER['REMOTE_PORT'] #用户连接到服务器时所使用的端口。 $_SERVER['SCRIPT_FILENAME'] #当前执行脚本的绝对路径名。 $_SERVER['SERVER_ADMIN'] #管理员信息 $_SERVER['SERVER_PORT'] #服务器所使用的端口 $_SERVER['SERVER_SIGNATURE'] #包含服务器版本和虚拟主机名的字符串。 $_SERVER['PATH_TRANSLATED'] #当前脚本所在文件系统(不是文档根目录)的基本路径。 $_SERVER['SCRIPT_NAME'] #包含当前脚本的路径。这在页面需要指向自己时非常有用。 $_SERVER['REQUEST_URI'] #访问此页面所需的 URI。例如,“/index.html”。
关闭与开启用户管理的功能的实现——
设置配置文件,给环境变量赋值
<?php
$pdo = new PDO("mysql:host=127.0.0.1;dbname=php","root","root");
$sql = "SELECT * FROM `system`";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$system = $stmt->fetch(PDO::FETCH_ASSOC);
$_ENV['closed'] = $system['closed']?1:0;
$_ENV['name'] = $system['name'];
$_ENV['address'] = $system['address'];
$_ENV['tel'] = $system['tel'];
?>为主页面增加开启与关闭的功能(通过if判断实现)
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>员工管理系统</title>
<style>
/*样式重置*/
h2, p, ul {
padding: 0;
margin: 0;
}
/*头部样式*/
.header {
height: 60px;
/*background-color: lightblue;*/
border-bottom: 1px solid #333;
line-height: 60px;
}
.header .content {
width: 1000px;
/*background-color: lightgray;*/
overflow: hidden;
margin: 0 auto;
}
.header .content h2 {
float: left
}
.header .content p {
float: right;
}
/*主体样式*/
.main {
width: 1000px;
min-height: 650px;
/*background-color: lightcyan;*/
margin: 0 auto;
position: relative;
}
.main .left {
width: 120px;
min-height: inherit;
/*background-color: lightgreen;*/
border-right: 1px solid #333;
position: absolute;
left: 0;
top: 0;
}
.main .right {
width: 880px;
min-height: inherit;
/*background-color: lightyellow;*/
position: absolute;
left: 121px;
top: 0;
}
/*左侧菜单样式*/
.main .left ul {
position: absolute;
left: 30px;
top: 50px;
}
.main .left li {
list-style-type: none;
line-height: 50px;
}
.main .left li a {
text-decoration-line: none;
}
.main .left li a:hover {
text-decoration-line: underline;
color: red;
}
/*右侧工作区样式*/
.main .right iframe {
width: 880px;
min-height: 650px;
border: none;
}
</style>
</head>
<?php
if (isset($_SESSION['username'])):
require_once 'config.php';
?>
<body>
<!--头部-->
<div class="header">
<div class="content">
<h2>员工管理系统</h2>
<p>管理员: <?php echo $_SESSION['username']; ?> | <a href="javascript:;"
onclick="confirm('是否退出')?location.href='logout.php':false;">退出</a>
</p>
</div>
</div>
<?php else: ?>
<script>location.href = "login.php";</script>
<?php endif; ?>
<!--中部-->
<div class="main">
<!--左侧菜单-->
<div class="left">
<ul>
<?php if ($_ENV['closed'] == 0):?>
<li><a href="staff_list.php" target="workspace">员工管理</a></li>
<?php endif;?>
<li><a href="system.php" target="workspace">系统设置</a></li>
<li><a href="ajax.php" target="workspace">用户设置</a></li>
</ul>
</div>
<!--右侧内容-->
<div class="right">
<iframe src="staff_list.php" name="workspace"></iframe>
<p style="text-align: center;margin-top: -100px;"><?php echo $_ENV['name'];?> 自主研发</p>
</div>
</div>
</body>
</html>注:$_ENV为环境变量,调用对应的环境变量只需连接定义所需环境变量的值即可。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号