php实现简单留言板功能
发布时间:2020-12-13 20:58:12 所属栏目:PHP教程 来源:网络整理
导读:首先创建消息表,其主要字段有发送者的名称,消息内容,以及消息发送时间; ? ? 然后在前端创建表单并将留言消息查询出来,进行列表展示,index.html ! DOCTYPE html html lang ="en" head meta charset ="UTF-8" title index / body form action ="./send_m
首先创建消息表,其主要字段有发送者的名称,消息内容,以及消息发送时间; ? ? 然后在前端创建表单并将留言消息查询出来,进行列表展示,index.html <!DOCTYPE html> <html lang="en"head> meta charset="UTF-8"title>index</bodyform action="./send_message.php" method="POST"> input type="text" name="sender" placeholder="你的昵称"><br> textarea rows="5" cols="22"="content"="留言内容"></textarea> button ="submit">发送button> form> table id="list" border="1" cellspacing="0" style="margin-top:20px;"> tr> th>ID>Name>Senderth class="content">Content>操作> tablescript src="https://cdn.staticfile.org/jquery/3.5.1/jquery.js"script> $.get('/test/main.php,function(data){ data = JSON.parse(data).data; var html = ''; $(data).each((index,item){ html+= ` <tr> td${item.id}/td> ${item.sender}${item.content}${item.send_time}a href"./update.php?id=${item.id}修改a> ./del.php?id=${item.id}删除td> tr> `; }); $(#list).append(html); }); html> ? 将表单提交过来的信息保存到数据库,send_message.php <?php $send_time = time(); $sender = $_POST['sender']; $content = $_POST['content'$con = mysqli_connect('localhost','root','123456'); if(!$con){ die('数据库连接失败').mysqli_error(); } mysqli_select_db($con,'test'mysqli_query('set names utf8'); $sql = "insert into message(sender,content,send_time) values('$sender','$content',1)">$send_time')"; $res = mysqli_query($sqlif($resecho '<script>alert("留言成功");window.location.href="index.html";</script>'; }else{ echo '<script>alert("留言失败");window.location.href="index.html";</script>'; } ? 从数据库读取数据,并返回json,main.php <?$sql = 'select * from message'$arr = []; while($row = mysqli_fetch_array($res,MYSQLI_ASSOC)){ $row['send_time'] = date('Y-m-d H:i:s',1)">$row['send_time']); $arr[] = $row; } if(isset()){ echo json_encode(array( 'code'=>0,'msg'=>'ok','data'=>$arr )); } )); } ?> ? 效果如下: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |