写一个php网页来操作keytxt文本需要在php网页中列举出keytxt下面的所有内容一行一个需要为每一行内容配置一个按钮用来删除keytxt里面对应的内容删除内容后网页自动刷新重新列举内容还需要一个添加编辑框和添加按钮点击按钮后将编辑框内的内容添加到keytxt里面添加后文本里面不能有空行添加的内容要在最后一行的下一行网页要美观色彩丰富页面控件等要能自适应手机和电脑宽度和高度
下面是一个基本的实现例子:
<!DOCTYPE html>
<html>
<head>
<title>Key.txt操作</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
padding: 20px;
}
.container {
max-width: 800px;
margin: 0 auto;
background-color: #fff;
padding: 20px;
}
h1 {
text-align: center;
color: #333;
}
.key-list {
margin-bottom: 20px;
}
.key-list button {
margin-left: 10px;
}
.add-key {
margin-bottom: 20px;
display: flex;
align-items: center;
}
.add-key input {
flex-grow: 1;
padding: 5px;
font-size: 14px;
}
.add-key button {
margin-left: 10px;
}
</style>
</head>
<body>
<div class="container">
<h1>Key.txt操作</h1>
<div class="key-list">
<?php
// 读取key.txt文件内容
$lines = file('key.txt');
// 列出每一行内容
foreach ($lines as $line) {
echo "<div>$line<button onclick=\"deleteKey('$line')\">删除</button></div>";
}
?>
</div>
<div class="add-key">
<input type="text" id="newKey" placeholder="添加内容">
<button onclick="addKey()">添加</button>
</div>
</div>
<script>
function deleteKey(key) {
// 发送删除请求到delete.php文件
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
// 删除成功后刷新页面
location.reload();
}
};
xhr.open("POST", "delete.php", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send("key=" + encodeURIComponent(key));
}
function addKey() {
var newKey = document.getElementById('newKey').value;
// 发送添加请求到add.php文件
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
// 添加成功后刷新页面
location.reload();
}
};
xhr.open("POST", "add.php", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send("key=" + encodeURIComponent(newKey));
}
</script>
</body>
</html>
然后,在同级目录下创建delete.php和add.php文件来处理删除和添加操作。
delete.php:
<?php
// 接收要删除的key
$key = $_POST['key'];
// 读取key.txt文件内容
$lines = file('key.txt');
// 打开key.txt文件以写入模式
$file = fopen('key.txt', 'w');
// 遍历每一行内容
foreach ($lines as $line) {
// 如果不是要删除的key,则写入到文件中
if (trim($line) !== trim($key)) {
fwrite($file, $line);
}
}
// 关闭文件
fclose($file);
?>
add.php:
<?php
// 接收要添加的key
$key = $_POST['key'];
// 打开key.txt文件以追加模式
$file = fopen('key.txt', 'a');
// 添加新的key到文件中
fwrite($file, $key . "\n");
// 关闭文件
fclose($file);
?>
请注意,以上代码仅提供了一个基本的实现示例,还可以根据需要进行改进和美化
AI 优化版
原文地址:https://www.cveoy.top/t/topic/hDYT 著作权归作者所有。请勿转载和采集!