php+ajax實(shí)現(xiàn)無刷新的新聞留言系統(tǒng)
本文介紹了一款無刷新的新聞留言系統(tǒng),最簡明易懂的一個(gè)ajax無刷新留言系統(tǒng),源碼中省略了接受數(shù)據(jù)驗(yàn)證的過程,大家可根據(jù)自己的需求進(jìn)行擴(kuò)展,下面進(jìn)入主題。

核心源碼:
1.配置文件:config.php,代碼如下:
<?php
//數(shù)據(jù)庫配置信息(用戶名,密碼,數(shù)據(jù)庫名,表前綴等)
$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接收數(shù)據(jù),只是演示效果,這里就省去驗(yàn)證了
$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(){
//只是說明原理,然后這里省去了驗(yàn)證文本框內(nèi)容的步驟,直接發(fā)送ajax請求
$.post("deal.php",{name : $("#name").val(), content : $("#content").val()}, function(data){
if(data.status){
var str = "<p><strong>"+data.name+"</strong> 發(fā)表了:"+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/>
內(nèi) 容:<textarea name="content" id="content"></textarea>
<input type="button" value="發(fā)布" 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> 發(fā)表了:".$row['content']."</p>";
}
?>
</div>
</body>
</html>
數(shù)據(jù)庫文件,代碼如下:
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實(shí)現(xiàn)無刷新的新聞留言系統(tǒng),希望對大家的學(xué)習(xí)有所幫助。
- PHP Ajax實(shí)現(xiàn)頁面無刷新發(fā)表評論
- PHP Ajax實(shí)現(xiàn)頁面無刷新發(fā)表評論
- 一個(gè)可分頁的基于文本的PHP留言板源碼
- 一個(gè)簡單的PHP&MYSQL留言板源碼
- 找到一款不錯(cuò)的基于AJAX留言板源碼(PHP版、ASP版)提供下載了
- 來自經(jīng)典的打造簡單的PHP&MYSQL留言板
- PHP+ajax 無刷新刪除數(shù)據(jù)
- php開發(fā)留言板的CRUD(增,刪,改,查)操作
- php簡單的留言板與回復(fù)功能具體實(shí)現(xiàn)
- php+ajax實(shí)現(xiàn)無刷新動態(tài)加載數(shù)據(jù)技術(shù)
相關(guān)文章
PHP 服務(wù)器配置(使用Apache及IIS兩種方法)
簡單介紹使用Apache及IIS解析PHP2009-06-06
PHP版微信第三方實(shí)現(xiàn)一鍵登錄及獲取用戶信息的方法
這篇文章主要介紹了PHP版微信第三方實(shí)現(xiàn)一鍵登錄及獲取用戶信息的方法,較為詳細(xì)的分析了微信第三方登陸的相關(guān)注意事項(xiàng)與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-10-10
php5.3中連接sqlserver2000的兩種方法(com與ODBC)
有時(shí)候我們需要在php5.3環(huán)境中使用sqlserver2000,這里簡單的介紹下,我們知道新版的php5.3是不支持mysql自帶的mssql_connect這個(gè)數(shù)據(jù)連接函數(shù),如果需要連接sqlserver數(shù)據(jù)庫,我們只能使用了com 接口來做2012-12-12
PHP在不同頁面間傳遞Json數(shù)據(jù)示例代碼
本文為大家介紹下PHP如何在不同頁面間傳遞Json數(shù)據(jù),具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈,希望對大家有所幫助2013-06-06

