首页 > 开发 > PHP > 正文

php+ajax制作无刷新留言板

2024-05-04 22:34:30
字体:
来源:转载
供稿:网友

本文就是和大家分享一款由php结合ajax实现的无刷新留言板,先给大家看一下最后的效果图:

数据库连接代码如下:

<?php$conn = @mysql_connect("localhost","root","root") or die ("MySql连接错误");mysql_select_db("demo",$conn);mysql_query("set names 'utf8'");?>

index.php文件代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><link href="bbs.css" type="text/css" rel="stylesheet"><title>无刷新显示回帖</title><script src="bbs.js" type="text/javascript"></script></head><body><h1>无刷新显示回帖</h1><div id="thread"><?phpinclude("conn.php");$sql = "select * from `bbs_post` where `threadid` ='1' order by id asc";$query =mysql_query($sql);while($row = mysql_fetch_array($query)){ ?> <div class="post" id="post<?php echo $row['id'];?>">    <div class="post_title"><?php echo $row['title'];?> [<?php echo $row['username'];?>]</div>    <div class="post_content"><pre><?php echo $row['content'];?></pre></div>   </div><?php}?></div><table class="reply"><tr> <td colspan="2" class="title">回帖<input type="hidden" name="threadid" id="threadid" value="1"></td></tr><tr> <td>姓名:</td> <td><input type="text" name="username" id="username"></td></tr><tr> <td>标题:</td> <td><input type="text" name="post_title" id="post_title"></td></tr><tr> <td>内容:</td> <td><textarea name="post_content" id="post_content"></textarea></td></tr><tr> <td colspan="2"><input type="button" onclick="submitPost()" value="提交"></td></tr></table></body></html> 

bbspost.php文件代码如下

<?phpinclude("conn.php");$title = $_POST["title"]; //获取title$content = $_POST["content"]; //获取content$username = $_POST["username"]; //获取username$threadId = $_POST["threadid"]; //获取threadid$sql="insert into bbs_post (title,content,username,threadid) " . "values ('$title','$content','$username','$threadId')"; mysql_query($sql); //传回最后一次使用 INSERT 指令的 ID$responseId=mysql_insert_id();echo $responseId;?>

bbs.js文件里面包括了大量ajax文件,代码如下

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表