写一个php网页来操作keytxt文本需要在php网页中列举出keytxt下面的所有内容一行一个需要为每一行内容配置一个按钮用来删除keytxt里面对应的内容删除内容后网页自动刷新重新列举内容还需要一个添加编辑框和添加按钮点击按钮后将编辑框内的内容添加到keytxt文件里面一行一个用换行符分割网页要美观色彩丰富页面控件等要能自适应手机和电脑宽度和高度只用一个php文件
以下是一个满足您需求的PHP网页代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>操作key.txt文本</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
margin: 0;
padding: 20px;
}
h1 {
color: #333333;
}
.container {
background-color: #ffffff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
}
.text-input {
width: 100%;
padding: 10px;
border: 1px solid #cccccc;
border-radius: 5px;
margin-bottom: 10px;
}
.button {
background-color: #4CAF50;
color: #ffffff;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}
.button:hover {
background-color: #45a049;
}
.list-item {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
}
.list-item span {
flex-grow: 1;
}
.list-item button {
background-color: #f44336;
color: #ffffff;
padding: 5px 10px;
border: none;
border-radius: 5px;
cursor: pointer;
}
.list-item button:hover {
background-color: #d32f2f;
}
</style>
</head>
<body>
<div class="container">
<h1>操作key.txt文本</h1>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input class="text-input" type="text" name="newKey" placeholder="输入要添加的内容">
<button class="button" type="submit">添加</button>
</form>
</div>
<?php
function readKeysFromFile() {
$keys = [];
if (file_exists('key.txt')) {
$file = fopen('key.txt', 'r');
while (($line = fgets($file)) !== false) {
$keys[] = trim($line);
}
fclose($file);
}
return $keys;
}
function writeKeysToFile($keys) {
$file = fopen('key.txt', 'w');
foreach ($keys as $key) {
fwrite($file, $key . PHP_EOL);
}
fclose($file);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$newKey = trim($_POST['newKey']);
if (!empty($newKey)) {
$keys = readKeysFromFile();
$keys[] = $newKey;
writeKeysToFile($keys);
$_POST['newKey'] = '';
header('Location: ' . $_SERVER['PHP_SELF']);
exit();
}
}
$keys = readKeysFromFile();
?>
<div class="container">
<h2>已有内容</h2>
<?php if (count($keys) > 0) : ?>
<?php foreach ($keys as $key) : ?>
<div class="list-item">
<span><?php echo $key; ?></span>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="deleteKey" value="<?php echo $key; ?>">
<button type="submit">删除</button>
</form>
</div>
<?php endforeach; ?>
<?php else : ?>
<p>没有内容。</p>
<?php endif; ?>
</div>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['deleteKey'])) {
$deleteKey = trim($_POST['deleteKey']);
if (!empty($deleteKey)) {
$keys = readKeysFromFile();
$keys = array_diff($keys, [$deleteKey]);
writeKeysToFile($keys);
header('Location: ' . $_SERVER['PHP_SELF']);
exit();
}
}
?>
</body>
</html>
将上述代码保存为一个php文件(例如index.php),然后将该文件放置在可以运行PHP的服务器上,访问该文件即可看到操作key.txt文本的网页。
该网页包含一个添加编辑框和添加按钮,用于添加内容到key.txt文件,以及列举已有内容,并为每一行内容配置了一个删除按钮,用于删除对应的内容。删除内容后,网页会自动刷新并重新列举内容。页面控件会自适应手机和电脑的宽度和高度,并且使用了一些简单的样式来美化页面
原文地址: https://www.cveoy.top/t/topic/hI8y 著作权归作者所有。请勿转载和采集!