Home > Backend Development > PHP Tutorial > php ajax no refresh message system

php ajax no refresh message system

墨辰丷
Release: 2023-03-30 09:58:02
Original
1636 people have browsed it

This article mainly introduces the php ajax non-refresh message system. Interested friends can refer to it. I hope it will be helpful to everyone.

This article introduces a non-refresh news message system, the most concise and easy-to-understand ajax non-refresh message system. The source code omits the process of accepting data verification. You can expand it according to your own needs. Below Go To Topic.

Core source code:

1. Configuration file: config.php, the code is as follows:

<?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"); 
?>
Copy after login

2. Process the request: deal.php, the code is as follows:

<?php 
 header("Content-type:text/html;charset=utf-8"); 
 include "config.php"; 
 //post接收数据,只是演示效果,这里就省去验证了 
 $name = $_POST[&#39;name&#39;]; 
 $content = $_POST[&#39;content&#39;]; 
 $sql = "insert into test (name,content) values (&#39;{$name}&#39;,&#39;{$content}&#39;);"; 
 $res = mysql_query($sql,$link); 
 if($res){ 
 echo &#39;{"name": "&#39;.$name.&#39;","content": "&#39;.$content.&#39;","status": "1"}&#39;; 
 } 
?>
Copy after login

3. Home page code: index.php, the code is as follows:

<!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("评论失败"); 
  } 
  }, &#39;json&#39;); 
 });   
}); 
</script> 
</head> 
<body> 
<p 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> 
</p> 
<p 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[&#39;name&#39;]."</strong> 发表了:".$row[&#39;content&#39;]."</p>"; 
 } 
?> 
</p> 
</body> 
</html>
Copy after login

Database file, the code is as follows:

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;
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

php method to intercept specified video frames into pictures

Swoole extension installation method and detailed tutorial in PHP

PHP method to generate color QR code based on QRCODE

The above is the detailed content of php ajax no refresh message system. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template