温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在php中使用ajax实现一个无刷新的新闻留言系统

发布时间:2020-12-11 17:04:00 来源:亿速云 阅读:127 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关怎么在php中使用ajax实现一个无刷新的新闻留言系统,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

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://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" /> 
<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/> 
 内&nbsp;&nbsp;容:<textarea name="content" id="content"></textarea>&nbsp;&nbsp; 
 <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实现一个无刷新的新闻留言系统有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI