iT邦幫忙

1

DAY8.PHP-將留言內容儲存在伺服器

今天來到台東玩,不忘繼續寫一些筆記的文章,放輕鬆放輕鬆...台東真的是個不錯的地方
輸入留言傳送到board.txt的檔案中,所以要記得創一個board.txt的檔也記得將這個檔的權限改到能讀取和寫入。
另外就是json格式轉入PHP顯示所有留言內容,再將PHP陣列轉入json格式寫入留言。

1.指定讀取檔案:$file = 'board.txt';
2.指定檔案存在時:if(file_exists($file))
3.讀取檔案:file_get_contents($file)
4.JSON格式轉換成PHP:json_decode()
5.用$_GET輸入留言:$board[]=$_GET['message'];
6.將陣列轉成JSON格式:json_encode
7.將傳入的字串寫成指定的檔案:file_put_contents
8.顯示所有的內容:foreach($board as $message){...
input.php

<form action="output.php" method="get">
<p>請留言</p>
<input type="text" name="message">
<input type="submit" value="送出">
</form>
output.php

<?php
$file ='board.txt';
if(file_exists($file)){
    $board = json_decode(file_get_contents($file));
}
$board[] = $_GET['message'];
file_put_contents($file, json_encode($board));
foreach ($board as $message) {
    echo '<p>', $message ,'</p><hr>';
}
?>

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言