php+ajax实现无刷新的新闻留言系统
网络编程 2021-07-05 09:09www.168986.cn编程入门
这篇文章主要介绍了一款基于php+ajax无刷新的新闻留言系统实现过程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文介绍了一款无刷新的新闻留言系统,最简明易懂的一个ajax无刷新留言系统,源码中省略了接受数据验证的过程,大家可根据自己的需求进行扩展,狼蚁网站SEO优化进入主题。
核心源码
1.配置文件:config.php,代码如下
<?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,代码如下
<?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,代码如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://.w3./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://.w3./1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无刷新</title> <link href="css/css.css" type="text/css" rel="stylesheet" /> <style type="text/css"> body{color:#555;font-size:14px;padding:0;margin:0;} #form { background:#dedede; padding:10px 20px; width:300px;} #show{ background:#f6f6f6;padding:10px 20px; width:300px;} #show p{ margin:6px; font-size:13px; line-height:22px; border-bottom:1px dashed #cdcdcd;} </style> <script type="text/javascript" src="jquery-1.7.2.min.js"></script> <script type="text/javascript"> $(function(){ $("#sub").click(function(){ //只是说明原理,然后这里省去了验证文本框内容的步骤,直接发送ajax请求 $.post("deal.php",{name : $("#name").val(), content : $("#content").val()}, function(data){ if(data.status){ var str = "<p><strong>"+data.name+"</strong> 发表了"+data.content+"</p>"; $("#show").prepend(str); //在前面追加 }else{ alert("评论失败"); } }, 'json'); }); }); </script> </head> <body> <div id="form"> <form action="deal.php" method="get" id="suggest_form"> 用户名<input type="text" name="name" id="name" /><br/> 内 容<textarea name="content" id="content"></textarea> <input type="button" value="发布" id="sub" /> </form> </div> <div id="show"> <?php include "config.php"; $sql = "select from test;"; $res = mysql_query($sql,$link); while($row=mysql_fetch_array($res)){ echo "<p><strong>".$row['name']."</strong> 发表了".$row['content']."</p>"; } ?> </div> </body> </html>
数据库文件,代码如下:
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实现无刷新的新闻留言系统,希望对大家的学习有所帮助。
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指