写一个php网页来操作keytxt文本需要在php网页中列举出keytxt下面的所有内容一行一个需要为每一行内容配置一个按钮用来删除keytxt里面对应的内容删除内容后网页自动刷新重新列举内容还需要一个添加编辑框和添加按钮点击按钮后将编辑框内的内容添加到keytxt里面keytxt里面不能有空行网页要美观色彩丰富页面控件等要能自适应手机和电脑宽度和高度
以下是一个实现上述要求的PHP网页示例:
<!DOCTYPE html>
<html>
<head>
<title>Key Management</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
padding: 20px;
}
h1 {
color: #333;
}
table {
margin: 0 auto;
border-collapse: collapse;
}
table td, table th {
padding: 10px 20px;
border: 1px solid #ccc;
}
input[type="text"] {
padding: 10px;
font-size: 16px;
border: none;
border-radius: 4px;
box-shadow: 0 0 5px #ccc;
}
input[type="submit"] {
padding: 10px 20px;
font-size: 16px;
background-color: #4CAF50;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h1>Key Management</h1>
<?php
// 处理删除按钮点击事件
if (isset($_GET['delete'])) {
$deleteKey = $_GET['delete'];
$keys = file('key.txt', FILE_IGNORE_NEW_LINES);
$keys = array_diff($keys, [$deleteKey]);
file_put_contents('key.txt', implode(PHP_EOL, $keys));
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}
// 处理添加按钮点击事件
if (isset($_POST['add'])) {
$newKey = trim($_POST['key']);
if (!empty($newKey)) {
file_put_contents('key.txt', $newKey . PHP_EOL, FILE_APPEND);
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}
}
// 读取key.txt文件内容
$keys = file('key.txt', FILE_IGNORE_NEW_LINES);
?>
<table>
<tr>
<th>Key</th>
<th>Action</th>
</tr>
<?php foreach ($keys as $key) { ?>
<tr>
<td><?php echo $key; ?></td>
<td>
<form method="get">
<input type="hidden" name="delete" value="<?php echo $key; ?>">
<input type="submit" value="Delete">
</form>
</td>
</tr>
<?php } ?>
</table>
<h2>Add Key</h2>
<form method="post">
<input type="text" name="key" placeholder="Enter key">
<input type="submit" name="add" value="Add">
</form>
</body>
</html>
将上述代码保存为index.php文件,并将key.txt文件与index.php文件放在同一个目录下。然后通过浏览器访问index.php文件,即可看到实现了要求的PHP网页。
网页使用了HTML和CSS来美化页面,使用了PHP来处理按钮点击事件、读取和写入key.txt文件的内容
AI 优化版
原文地址:https://www.cveoy.top/t/topic/hDYx 著作权归作者所有。请勿转载和采集!