写一个php网页来操作keytxt文本需要在php网页中列举出keytxt下面的所有内容一行一个需要为每一行内容配置一个按钮用来删除keytxt里面对应的内容删除内容后网页自动刷新重新列举内容还需要一个添加编辑框和添加按钮点击按钮后将编辑框内的内容添加到keytxt里面添加后文本里面不能有空行添加的内容要在最后一行的下一行网页要美观色彩丰富页面控件等要能自适应手机和电脑宽度和高度只用一个php文件
以下是一个满足需求的PHP网页示例:
<!DOCTYPE html>
<html>
<head>
<title>操作key.txt</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
margin: 0;
padding: 20px;
}
h1 {
color: #333;
}
.content {
background-color: #fff;
padding: 20px;
margin-top: 20px;
}
.button {
display: inline-block;
padding: 5px 10px;
background-color: #428bca;
color: #fff;
border: none;
cursor: pointer;
}
.button:hover {
background-color: #3071a9;
}
.add-form {
margin-top: 20px;
}
.add-form input[type="text"] {
width: 200px;
padding: 5px;
font-size: 14px;
}
.add-form input[type="submit"] {
padding: 5px 10px;
background-color: #5cb85c;
color: #fff;
border: none;
cursor: pointer;
}
.add-form input[type="submit"]:hover {
background-color: #449d44;
}
</style>
</head>
<body>
<h1>操作key.txt</h1>
<?php
// 处理删除按钮点击事件
if (isset($_GET['delete'])) {
$file = fopen("key.txt", "r");
$lines = array();
while (!feof($file)) {
$line = fgets($file);
if (trim($line) != $_GET['delete']) {
$lines[] = $line;
}
}
fclose($file);
$file = fopen("key.txt", "w");
fwrite($file, implode("", $lines));
fclose($file);
// 刷新页面
header("Location: ".$_SERVER['PHP_SELF']);
exit;
}
// 处理添加按钮点击事件
if (isset($_POST['add'])) {
$content = trim($_POST['content']);
if (!empty($content)) {
$file = fopen("key.txt", "a");
fwrite($file, $content."\n");
fclose($file);
}
// 刷新页面
header("Location: ".$_SERVER['PHP_SELF']);
exit;
}
// 读取key.txt内容并列举
$file = fopen("key.txt", "r");
echo '<div class="content">';
echo '<h2>当前内容:</h2>';
echo '<ul>';
while (!feof($file)) {
$line = fgets($file);
if (trim($line) != "") {
echo '<li>'.$line.'<button class="button" onclick="deleteContent(\''.$line.'\')">删除</button></li>';
}
}
echo '</ul>';
echo '</div>';
fclose($file);
?>
<div class="add-form">
<h2>添加内容:</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="content" placeholder="输入要添加的内容">
<input type="submit" name="add" value="添加">
</form>
</div>
<script>
function deleteContent(content) {
if (confirm("确定要删除该内容吗?")) {
window.location.href = "<?php echo $_SERVER['PHP_SELF']; ?>?delete=" + encodeURIComponent(content);
}
}
</script>
</body>
</html>
将上述代码保存为一个.php文件,例如key.php,然后将文件放置在一个能运行PHP的服务器上,通过访问该文件即可使用操作key.txt文本的功能
AI 优化版
原文地址:https://www.cveoy.top/t/topic/hDZe 著作权归作者所有。请勿转载和采集!