php+ajax實現無刷新的新聞留言系統

文章主要介紹了一款基於php+ajax無刷新的新聞留言系統實現過程,感興趣的小夥伴們可以參考一下。

php+ajax實現無刷新的新聞留言系統

本文介紹了一款無刷新的新聞留言系統,最簡明易懂的一個ajax無刷新留言系統,源碼中省略了接受數據驗證的.過程,大家可根據自己的需求進行擴展,下面進入主題。

核心源碼:

1.配置文件:,代碼如下:

?

1

2

3

4

5

6

7

8

9

10

11

<?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.處理請求:,代碼如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

<?php

header("Content-type:text/html;charset=utf-8");

include "";

//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.首頁代碼:,代碼如下:

?

1

2

3

4

5

6