HTML+CSS+PHP实现网页留言板功能(含代码,需要创建数据库)

1.body部分效果展示(不包括footer)

e7763df6b58149fb89f51fbcb00400ad.png

8d8e22dfce9847448c6ddc8dca1f69d6.jpeg

2、代码

2.1 leaving.php(看到的网页)

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>留言板</title>  
    <link rel="stylesheet" href="leaving_styles.css">  
</head>  
<body>  
    <header >  
        <h1>…………</h1>  
    </header> 
    
  <div class="le">
    <h2>留言板</h2>  
    <div class="in">
    <form action="post_message.php" method="post">  
        <textarea name="message" rows="5"  placeholder="输入你的留言..."></textarea> 
        <br>
        <input class="in" type="submit" value="提交">  
    </form> 
    </div>
    
    <table> 
        <tr><td>
        <div id="message-container" >  
        <?php include 'display_messages.php'; ?>  
        </div>
        </td></tr>
    </table>
  </div>
    <footer>  
        <p>留言板 &copy; 2024 草海桐</p>  
    </footer>  
</body>  
</html>

2.2 leaving_styles.css

body {  
    font-family: Arial, sans-serif;  
    margin: 0;  
    padding: 0; 
    justify-content: center;  
    align-items: center;  
    background-color: rgba(224, 255, 255, 1);
}  
  
header {  
    width: 100%;
    height: 200px;
    background-image: url('.jpg');  
    background-size: cover; 
    background-position: center; 
    text-align: center;
    margin: auto;
} 

h1{

}

h2{
    display: flex;
    justify-content: center; 
    padding: 0;
    margin: 0;
}

/*.le{
    margin: 0; 
    padding: 20px; 
    text-align: center;
    align-items: center;
    background-color: rgba(224, 255, 255, 1);    
}*/
.le{
    width: auto;
    top: 200px;
    bottom: 0;
    left: 0;
    text-align: center;
    align-items: center;
    margin: 0;
    padding: 10px;
    background-size:cover;
    position: absolute;
    right: 0;
}   
.in{
    justify-content: center;
}
textarea{
    width: 65%;
}


form{
    text-align: center;
    padding: 10px; 
    align-items: center;
}
table{
    border-collapse: collapse; 
    align-items: center;
    text-align: center;
    bottom: 0;
} 
tr{
    align-items: center;
    text-align: center;
}
td{
    text-align:center ;
    align-items: center;
}
#message-container{  
    text-align: center;
    align-items: center;
    max-height: 350px;
    overflow-y: auto; 
    padding: 0;
    border: 2px solid black; 
    background-color: rgba(255, 255, 255, 1);  
}

@media screen and (max-width: 400px) {  
    #message-container{  
    align-items: center;
    text-align: center;
    max-height: 220px;
    width: auto;
    overflow-y: auto; 
    padding: 0;
    border: 2px solid black; 
    background-color: rgba(255, 255, 255, 1);  
    }
}

footer {  
    background-color: #333;  
    color: #fff;  
    text-align: center;  
    position: fixed;  
    left: 0;  
    bottom: 0;  
    width: 100%;  
}  

2.3 post_massage.php

<?php  
$servername = "localhost"; 
$username = "";//用户名
$password = "";//密码
$dbname = "";//数据库名称
  
$conn = new mysqli($servername, $username, $password, $dbname);  
  
if ($conn->connect_error) {  
    die("连接失败: " . $conn->connect_error);  
}  
  
$message = $conn->real_escape_string($_POST['message']);  
  
$sql = "INSERT INTO messages (content) VALUES ('$message')";  
if ($conn->query($sql) === TRUE) {  
    echo "留言成功添加!";  
    header("Location: leaving.php");
    exit();  
} else {  
    echo "Error: " . $sql . "<br>" . $conn->error;  
}  
  
$conn->close();  
?>

2.4 display_massages.php

<?php  
$servername = "localhost";  
$username = "";//用户名  
$password = "";//密码
$dbname = "";//数据库名称

$conn = new mysqli($servername, $username, $password, $dbname);  
  
if ($conn->connect_error) {  
    die("连接失败: " . $conn->connect_error);  
}  
  
$sql = "SELECT * FROM messages ORDER BY timestamp DESC";  
$result = $conn->query($sql);  
  
if ($result->num_rows > 0) {  
    while ($row = $result->fetch_assoc()) {  
        echo "<p>提交时间:" . $row["timestamp"]  . "<br>". nl2br($row["content"])."<hr></p>";  
    }  
} else {  
    echo "目前还没有留言。";  
}  

$conn->close();  
?>

 

 

 

 

HTML留言板页面可以通过使用表单元素和一些简单的CSS样式来创建。以下是一个基本的HTML留言板页面的示例: ```html <!DOCTYPE html> <html> <head> <title>留言板</title> <style> .container { max-width: 600px; margin: 0 auto; padding: 20px; background-color: #f2f2f2; border: 1px solid #ccc; box-shadow: 0 0 10px #ccc; } form { display: flex; flex-direction: column; align-items: center; } input[type="text"], textarea { width: 100%; padding: 10px; margin-bottom: 10px; border: 1px solid #ccc; border-radius: 5px; box-sizing: border-box; } input[type="submit"] { background-color: #007bff; color: #fff; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer; } .message { margin-bottom: 20px; padding: 10px; background-color: #fff; border: 1px solid #ccc; border-radius: 5px; box-shadow: 0 0 10px #ccc; word-break: break-all; } .message p:first-child { font-weight: bold; margin-bottom: 5px; } .message p:last-child { font-style: italic; font-size: 12px; color: #999; } </style> </head> <body> <div class="container"> <h1>留言板</h1> <form action="#" method="post"> <label for="name">姓名:</label> <input type="text" id="name" name="name" required> <label for="email">邮箱:</label> <input type="text" id="email" name="email" required> <label for="message">留言:</label> <textarea id="message" name="message" rows="5" required></textarea> <input type="submit" value="提交留言"> </form> <div id="messages"> <!-- 这里是留言展示区 --> </div> </div> </body> </html> ``` 在这个示例中,我们使用了一个容器元素来包整个留言板页面。然后,我们定义了一个表单元素来收集用户的信息和留言。表单包括三个输入框:姓名、邮箱和留言内容,以及一个提交按钮。 我们使用一些CSS样式来美化页面的外观,包括设置容器的最大宽度、边框、背景颜色和阴影效果,以及设置输入框和留言展示区的样式。 在代码中,我们还包了一个div元素来展示留言。在实际应用中,我们需要使用服务器端脚本来将留言保存到数据库或文件中,并使用客户端脚本来从服务器获取留言并展示到页面上。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

草海桐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值