帮我使用html5实现一个日记网站,并将日记储存到名为"content.out"文件内
时间: 2023-06-09 15:02:44 浏览: 55
好的,我可以为您提供一些帮助。下面是一个简单的HTML5日记网站的示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My Diary</title>
</head>
<body>
<h1>My Diary</h1>
<form>
<label for="title">Title:</label>
<input type="text" id="title" name="title"><br><br>
<label for="date">Date:</label>
<input type="date" id="date" name="date"><br><br>
<label for="content">Content:</label><br>
<textarea id="content" name="content" rows="10" cols="50"></textarea><br><br>
<input type="button" value="Save" onclick="saveDiary()">
</form>
<script>
function saveDiary() {
var title = document.getElementById('title').value;
var date = document.getElementById('date').value;
var content = document.getElementById('content').value;
var data = {title: title, date: date, content: content};
var xhr = new XMLHttpRequest();
xhr.open('POST', 'save.php');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(data));
}
</script>
</body>
</html>
```
这个网站包含一个表单,其中包含标题,日期和内容字段,以及一个保存按钮。当用户点击保存按钮时,将调用JavaScript函数,该函数将从表单中获取输入,并将其作为JSON数据通过POST请求发送到服务器上的save.php脚本。
下面是一个简单的PHP脚本,用于储存提交的日记到"content.out"文件中:
```php
<?php
$data = json_decode(file_get_contents('php://input'), true);
$title = $data['title'];
$date = $data['date'];
$content = $data['content'];
$entry = "Title: $title\nDate: $date\nContent: $content\n\n";
file_put_contents('content.out', $entry, FILE_APPEND);
?>
```
该脚本将首先从POST请求的正文中获取JSON数据,然后解码它并将其保存到"content.out"文件中。
希望这可以为你提供一些帮助!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)