Home > Backend Development > PHP Tutorial > PHP+ajax implements a news message system without refreshing (with source code)_PHP tutorial

PHP+ajax implements a news message system without refreshing (with source code)_PHP tutorial

WBOY
Release: 2016-07-13 10:50:42
Original
947 people have browsed it

Ajax has been very simple and easy to implement since jquery was introduced. Now I will introduce the implementation process of a refresh-free news message system based on jquery ajax+php mysql. I hope this article can help you.

The most concise and easy-to-understand ajax message system without refreshing, the source code omits the process of accepting data verification. Readers can expand it according to their own needs.


Core source code:


1. Configuration file: config.php

The code is as follows Copy code
 代码如下 复制代码


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

$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");

?>

代码如下 复制代码


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"}';

}

?>

//Database configuration information (user name, password, database name, table prefix, etc.) $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. Process the request: deal.php
The code is as follows Copy code
<🎜> header("Content-type:text/html;charset=utf-8");<🎜> <🎜> include "config.php";<🎜> <🎜> //Post receives data, just to demonstrate the effect, no verification is needed here <🎜> <🎜> $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

 代码如下
 代码如下 复制代码

无刷新

 


 

  用户名:

  内 容:

  

 

 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;

无刷新  
 
  用户名:
  内  容:       
".$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实现无刷新的新闻留言系统下载

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632619.htmlTechArticleAjax has been very simple and easy to implement since jquery was introduced. Let me introduce an ajax+php based on jquery MySQL's non-refresh news message system implementation process, I hope this article can bring you...
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