• PHP自己的框架留言板功能实现


    目录

    1、实现留言板功能效果

     2、创建留言板数据表

    3、控制器提交和显示方法 

    4、提交留言板html 

    5、留言板列表html  


     

    1、实现留言板功能效果

     2、创建留言板数据表
    1. CREATE TABLE `msg` (
    2. `id` int(11) NOT NULL AUTO_INCREMENT,
    3. `name` varchar(30) DEFAULT NULL COMMENT '留言板姓名',
    4. `text` varchar(100) DEFAULT NULL COMMENT '留言板内容',
    5. `time` varchar(30) DEFAULT NULL COMMENT '添加时间',
    6. PRIMARY KEY (`id`)
    7. ) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
    3、控制器提交和显示方法 
    1. class indexCrl extends CrlBase {
    2. //留言板提交功能
    3. public function index(){
    4. if($_SERVER['REQUEST_METHOD'] == 'POST'){
    5. if($_POST['name'] && $_POST['text']){
    6. $data=[
    7. "name"=>$_POST['name'],
    8. "text"=>$_POST['text'],
    9. "time"=>date("Y-m-d H:i:s")
    10. ];
    11. $res= table("msg")->insert($data);
    12. if($res){
    13. echo json_encode(['status'=>1]);exit;
    14. }
    15. }
    16. echo json_encode(['status'=>0]);exit;
    17. }else{
    18. $this->display();
    19. }
    20. }
    21. //留言板列表
    22. public function lst(){
    23. $data= table("msg")->select();
    24. $this->assign('data',$data);
    25. $this->display();
    26. }
    27. }
    4、提交留言板html 
    1. <meta charset="UTF-8">
    2. html>
    3. <html>
    4. <head>
    5. <title>留言板title>
    6. head>
    7. <body>
    8. <h1>留言板h1>
    9. <form id="message-form">
    10. <label for="name">姓名:label>
    11. <input type="text" id="name" required><br><br>
    12. <label for="message">留言内容:label><br>
    13. <textarea id="message" required>textarea><br><br>
    14. <button type="submit">提交留言button>
    15. form>
    16. <div id="message-list">div>
    17. <script typet="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js">script>
    18. <script>
    19. // 当表单提交时
    20. document.getElementById("message-form").addEventListener("submit", function(e) {
    21. e.preventDefault(); // 阻止表单默认的提交行为
    22. // 获取用户输入的姓名和留言内容
    23. var name = document.getElementById("name").value;
    24. var message = document.getElementById("message").value;
    25. $.ajax({
    26. dataType: 'json',
    27. type: 'POST',
    28. url: '',
    29. data: {
    30. name: name,
    31. text: message
    32. },
    33. success: function (json) {
    34. if(json.status==1){
    35. alert('提交成功');
    36. // 创建一个新的留言元素
    37. var newMessage = document.createElement("div");
    38. newMessage.innerHTML = "" + name + ":" + message;
    39. // 将新留言添加到留言列表中
    40. document.getElementById("message-list").appendChild(newMessage);
    41. // 清空表单输入
    42. document.getElementById("name").value = "";
    43. document.getElementById("message").value = "";
    44. }else{
    45. alert('提交失败');
    46. }
    47. },
    48. error: function (jqXHR, textStatus, errorThrown) {
    49. alert('系统错误');
    50. }
    51. });
    52. });
    53. script>
    54. body>
    55. html>
    5、留言板列表html  
    1. html>
    2. <html>
    3. <head>
    4. <title>留言板title>
    5. <style>
    6. table {
    7. border-collapse: collapse;
    8. width: 100%;
    9. }
    10. th, td {
    11. padding: 8px;
    12. text-align: left;
    13. border-bottom: 1px solid #ddd;
    14. }
    15. th {
    16. background-color: #f2f2f2;
    17. }
    18. style>
    19. head>
    20. <body>
    21. <h1>留言板h1>
    22. <table id="message-table">
    23. <tr>
    24. <th>姓名th>
    25. <th>留言内容th>
    26. <th>时间th>
    27. tr>
    28. {foreach $data as $key=>$item}
    29. <tr>
    30. <td>{$item['name']}td>
    31. <td>{$item['text']}td>
    32. <td>{$item['time']}td>
    33. tr>
    34. {/foreach}
    35. table>
    36. body>
    37. html>

  • 相关阅读:
    Java中去掉字符串中的非中文字符
    正确使用自旋锁、互斥锁
    mac如何在item2中展示git分支
    Navicat向mysql数据库中导入sql文件常见报错问题总结及解决办法
    对自动化测试的一些展望与理解
    AES和SM4各有什么特点?有什么不同之处?
    PyTorch实现苹果M1芯片GPU加速:训练速度提升7倍,性能最高提升21倍
    redis---分布式锁存在的问题及解决方案(Redisson)
    在CentOS7上增加swap空间
    开源模型应用落地-qwen2模型小试-入门篇(六)
  • 原文地址:https://blog.csdn.net/weixin_39934453/article/details/132719929