现在的位置: 首页 -> PHP学习 -> AJAX实例 -> 精简版php+ajax实现无刷新的新闻留言系统

精简版php+ajax实现无刷新的新闻留言系统

2013-07-26 21:43评论数 6 ⁄ 被浏览 25498 views+

最简明易懂的一个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>


源码和数据库打包下载:ajax无刷新.zip  

大小:35.5KB


 

文章出自:https://www.daixiaorui.com/read/11.html 本站所有文章,除注明出处外皆为原创,转载请注明本文地址,版权所有。

目前有 6 条评论  @我要评论

  1. 6楼青鸟 : 2016-11-02 09:13:48 评论说: @回复

    $sql = "select * from test order by id desc;"; 感觉下面显示留言列表的时候sql给个降序排列会更好些

  2. 5楼袁志蒙 : 2015-08-12 17:40:05 评论说: @回复

    我咋不能用呢

  3. 4楼小小的 : 2015-07-21 16:05:28 评论说: @回复

    很不错

  4. 3楼asd : 2015-02-13 11:03:59 评论说: @回复

    谢谢, 你写的代码很好理解

  5. 2楼2498 : 2014-02-24 15:24:13 评论说: @回复

    不能无刷新显示你刚刚提交的留言

    回复留言标识 Dai 回复 2498: 请问你用的什么浏览器,可能是不兼容吧

    2014-02-24 22:54:16  @回复

    回复留言标识 dove 回复 darex: 你的意思不还是不太懂

    2015-02-17 22:07:40  @回复

    回复留言标识 dove 回复 dove: 自己回复一下自己的说法

    2015-02-18 03:09:46  @回复

  6. 1楼darex : 2013-07-27 20:31:15 评论说: @回复

    自己先占个沙发吧,同时欢迎大家前来评论!

    回复留言标识 dove 回复 darex:喜欢的的说法

    2015-02-18 03:09:14  @回复

您的每一个评论都是对我的一份支持

 博客二维码

昵称 *

邮箱 *