一聚教程网:一个值得你收藏的教程网站

热门教程

php+ajax实现无刷新的新闻留言系统(附源码)

时间:2022-06-24 15:30:34 编辑:袖梨 来源:一聚教程网

最简明易懂的一个ajax无刷新留言系统了,源码中省略了接受数据验证的过程。读者可根据自己的需求进行扩展。


核心源码:


1.配置文件:config.php

 代码如下 复制代码


 //数据库配置信息(用户名,密码,数据库名,表前缀等)

 $cfg_dbhost = "localhost";

 $cfg_dbuser = "root";

 $cfg_dbpwd = "root";

 $cfg_dbname = "ajaxdemo1";

 $cfg_dbprefix = "";

 


 $link = mysql_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd);

 mysql_select_db($cfg_dbname);

 mysql_query("set names utf8");

?>

2.处理请求:deal.php

 代码如下 复制代码


 header("Content-type:text/html;charset=utf-8");

 include "config.php";

 //post接收数据,只是演示效果,这里就省去验证了

 $name = $_POST['name'];

 $content = $_POST['content'];

 


 $sql = "insert into test (name,content) values ('{$name}','{$content}');";

 $res = mysql_query($sql,$link);

 if($res){

  echo '{"name": "'.$name.'","content": "'.$content.'","status": "1"}';

 }

?>

3.首页代码:index.php

 代码如下 复制代码

无刷新

 


 

  用户名:

  内  容:<textarea name="content" id="content">  

  

 

 include "config.php";

 $sql = "select * from test;";

 $res = mysql_query($sql,$link);

 while($row=mysql_fetch_array($res)){

  echo "

".$row['name']." 发表了:".$row['content']."

";

 }

?>

数据库文件

 代码如下 复制代码


DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(64) NOT NULL,
  `content` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

源码下载地址:php+ajax实现无刷新的新闻留言系统下载

热门栏目