php实战:html+php+mysql实现留⾔板功能功能描述:
1)实测链接的是远程数据库
2)打开留⾔板加载留⾔
3)新添加的留⾔显⽰在下⽅
css代码,⽤于设置弹框和新添加的留⾔样式等
<style type="text/css">
#comments{
margin:10px auto;
}
#post{
margin-top:10px;
}
#comments p,
#post p{
line-height:30px;
}
#comments p span{
margin:4px;
color:#bdb8b8;
}
php远程连接mysql数据库#message{
position: absolute;
top: 40%;
left: 100px;
width: 200px;
height: 50px;
background: #f2f2f2;
border: 1px solid;
border-radius: 3px;
line-height: 50px;
text-align: center;
display: none;
}
</style>
html代码:
<div>
<div id="post">
<h3>⽂章评论</h3>
<p>昵称:</p>
<p><input type="text" class="input" id="user" /></p>
<p>评论内容:</p>
<p><textarea class="input" id="txt" ></textarea></p>
<p><input type="submit" class='btn'value="发表" id="add" /></p>
<div id="message"></div>
</div>
<div id="comments"></div>
</div>
js代码,⽤于加载留⾔
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(function() {
var comments = $("#comments");
$.getJSON("data.php", function(json) {
$.each(json, function(index, array) {
var txt = "<p><strong>" + array["user"] + "</strong>:" + array["comment"] + "<span>" + array["addtime"] + "</span></p>"; comments.append(txt);
});
});
//将评论的内容展出
$("#add").click(function() {
var user = $("#user").val();
var txt = $("#txt").val();
$.ajax({
type: "POST",
url: "comment.php",
data: "user=" + user + "&txt=" + txt,
dataType : 'JSON',
success: function(res) {
if (de == 1) {
var str = "<p><strong>" + res.user + "</strong>:" + + "<span>刚刚</span></p>";
comments.append(str);
$("#message").show().html("发表成功!").fadeOut(1000);
$("#txt").attr("value", "");
} else {
$("#message").show().ssage).fadeOut(1000);
}
}
});
});
});
</script>
connect.php⽂件,⽤于连接数据库
<?php
$host="your db host";
$db_user="your db";
$db_pass="your db password";
$db_name="your db name";
$timezone="Asia/Shanghai";
$link=mysqli_connect($host,$db_user,$db_pass);//连接数据库主机
mysqli_select_db($link,$db_name);//选择数据库
mysqli_query($link,"SET names UTF8");//设置数据库编码格式
header("Content-Type: text/html; charset=utf-8");//设置头部样式
date_default_timezone_set($timezone); //北京时间
data.php,⽤于打开⽹页时展⽰留⾔
<?php
include_once("connect.php");//连接数据库
$q=mysqli_query($link,"select * from comments");//获取数据库的数据
while($row=mysqli_fetch_array($q)){
$comments[] = array("id"=>$row['id'],"user"=>$row['user'],"comment"=>$row['comment'],"addtime"=>$row['addtime']);
}
echo json_encode($comments);//以json格式编码
comment.php,⽤于将新的留⾔存⼊数据库并展⽰在视图上
<?php
include_once("connect.php");
//trim,返回字符串 str 去除⾸尾空⽩字符后的结果。如果不指定第⼆个参数,则去除普通控制,制表符,换⾏符,回车符,空字节符,垂直制表符//htmlspecialchars() 函数把预定义的字符转换为 HTML 实体
$user = htmlspecialchars(trim($_POST['user']));
$txt = htmlspecialchars(trim($_POST['txt']));
if(empty($user)){
$data = array("code"=>355,"message"=>"昵称不能为空!");
echo json_encode($data);
exit;
}
if(empty($txt)){
$data = array("code"=>356,"message"=>"内容不能为空");
echo json_encode($data);
exit;
}
$time = date("Y-m-d H:i:s");
$query=mysqli_query($link,"insert into comments(user,comment,addtime)values('$user','$txt','$time')");
if($query) {
$data = array("code" => 1, "message"=>"success","user" => $user , "txt" => $txt);
echo json_encode($data);
}
另外,还有在数据库中创建comment的sql命令
CREATE TABLE `comments`(
`id` int(4) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`user` varchar(50),
`comment` varchar(200),
`addtime` datetime not null
)engine=MYISAM CHARACTER SET UTF8 COLLATE utf8_unicode_ci;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论