登入之後,透過URI傳遞使用者名稱,使用者名稱會被存在$_GET陣列中。
board.php
<?php
include 'style.html';
$name = $_GET['name'];
?>
<title>Board</title>
<body>
<div class="flex-center position-ref full-height">
<div class="top-right home">
<a href='view.php?name=<?=$name?>'>View</a>
<a href="index.php">Logout</a>
<a href="signup.php">Register</a>
</div>
<div class="content">
<div class="m-b-md">
<form name="form1" action="board.php" method="post">
<input type="hidden" name="name" value="<?=$name?>">
<p><strong><?="Hi, " . $name?></strong></p>
<p>SUBJECT</p>
<p><input type="text" name="subject"></p>
<p>CONTENT</p>
<p><textarea style="font-family: 'Nunito', sans-serif; font-size:20px; width:550px;height:100px;" name="content"></textarea></p>
<p><input type="submit" name="submit" value="SEND">
<style>
input {padding:5px 15px; background:#FFCCCC; border:0 none;
cursor:pointer;
-webkit-border-radius: 5px;
border-radius: 5px; }
</style>
<input type="reset" name="Reset" value="RESET">
<style>
input {
padding:5px 15px;
background:#FFCCCC;
border:0 none;f
cursor:pointer;
-webkit-border-radius: 5px;
border-radius: 5px;
font-family: 'Nunito', sans-serif;
font-size: 19px;
}
</style>
</form>
</div>
</body>
</html>
<?php
//送出留言後會執行下面這段程式碼
if (isset($_POST['submit'])) {
include "db.php";
echo '<div class="success">Added successfully !</div>';
$name = $_POST['name'];
$subject = $_POST['subject'];
$content = $_POST['content'];
$sql = "INSERT guestbook(name, subject, content, time) VALUES ('$name', '$subject', '$content', now())";
if (!mysqli_query($db, $sql)) {
die(mysqli_error());
} else {
//若成功將留言存進資料庫,會自動跳轉到顯示留言的頁面
echo "
<script>
setTimeout(function(){window.location.href='view.php?name=" . $name . "';},500);
</script>";
}
} else {
echo '<div class="success">Click <strong>Send</strong> when you\'re done.</div>';
}
?>
如果不想讓參數赤裸裸的顯示在網址列上,也可以將form表單的欄位隱藏起來,
按下submit送出資料時,這個欄位的資料也會在$_POST陣列中。
<input type="hidden" name="name" value="<?=$name?>">
可以在類型為hidden的欄位加入參數,而使用者介面上是看不到這欄的。
另外,這個範例延遲跳轉頁面是使用javascript來完成的,但其實PHP也有函式能夠達成相同效果。
sleep($要延遲的秒數)
這邊會依照URL所傳遞的使用者的名稱和留言者是否一致,來檢查哪些是他留下的留言,只有樓主能編輯或刪除自己的留言。
view.php
<title>All messages</title>
<?php
include 'style.html';
$name = $_GET['name'];
?>
<body>
<div class="flex-center position-ref full-height">
<div class="top-right home">
<?php
if (!$name) {
echo '<a href="index.php">Log in</a>';
} else {
echo "<a href='board.php?name=" . $name . "'>Write some messages</a>";
echo '<a href="index.php">Log out</a>';
}?>
</div>
</div>
<div class="note full-height">
<?php
session_start();
include "db.php";
$sql = "select * from guestbook";
$result = mysqli_query($db, $sql);
$_SESSION['name'] = $name = $_GET['name'];
//從資料庫中撈留言紀錄並顯示出來
while ($row = mysqli_fetch_assoc($result)) {
echo "<br>Visitor Name:" . $row['name'];
echo "<br>Subject:" . $row['subject'];
echo "<br>Content:" . nl2br($row['content']) . "<br>";
if ($name == $row['name']) { //若登入者名稱和留言者名稱一致,顯示出編輯和刪除的連結
echo '
<a href=" edit.php?no=' . $row['no'] . '&name=' . $name . '">
Edit message content</a> | <a href="delete.php?no=' . $row['no'] . '">Delete the message</a><br>';
}
echo "Time:" . $row['time'] . "<br>";
echo "<hr>";
}
echo "<br>";
echo '<div class="bottom left position-abs content">';
echo "There are " . mysqli_num_rows($result) . " messages.";
?>
</div>
</body>
</html>