以下是一个简单的PHP记事本的源代码示例:
index.php:
<!DOCTYPE html>
<html>
<head>
<title>PHP记事本</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<h1>PHP记事本</h1>
<form action="save_note.php" method="POST">
<textarea name="note" placeholder="在这里输入你的笔记"></textarea>
<button type="submit">保存</button>
</form>
<h2>已保存的笔记:</h2>
<div class="notes">
<?php
// 读取保存的笔记
$notes = file_get_contents('notes.txt');
if (!empty($notes)) {
$notes = explode("\n", $notes);
foreach ($notes as $note) {
echo "<div class='note'>" . $note . "</div>";
}
}
?>
</div>
</div>
</body>
</html>
save_note.php:
<?php
// 获取提交的笔记内容
$note = $_POST['note'];
// 保存笔记到文件
$file = fopen('notes.txt', 'a');
fwrite($file, $note . "\n");
fclose($file);
// 重定向回主页
header('Location: index.php');
?>
style.css:
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
}
h1 {
text-align: center;
}
form textarea {
width: 100%;
height: 100px;
margin-bottom: 10px;
}
form button {
display: block;
margin: 0 auto;
}
h2 {
margin-top: 20px;
}
.notes {
border: 1px solid #ccc;
padding: 10px;
}
.note {
margin-bottom: 10px;
}
这个记事本应用使用一个文本文件来保存笔记。用户可以在文本框中输入笔记,然后点击保存按钮将笔记保存到文件中。已保存的笔记将在页面上显示出来。
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站