shutbox jquery无刷新发布评论
shutbox jquery无刷新发布评论
本例的数据库很简单,一看就会明白
XML/HTML Code
- <form method="post" id="form">
- <table cellpadding="0" cellspacing="0">
- <tr>
- <td><label>User</label></td>
- <td><input class="text user" id="nick" type="text" MAXLENGTH="25" /></td>
- </tr>
- <tr>
- <td><label>Message</label></td>
- <td><input class="text" id="message" type="text" MAXLENGTH="255" /></td>
- </tr>
- <tr>
- <td></td>
- <td><input id="send" type="submit" value="Shout it!" /></td>
- </tr>
- </table>
- </form>
- <div id="container">
- <ul class="menu">
- <li>Shoutbox</li>
- </ul>
- <span class="clear"></span>
- <div class="content">
- <h1>Latest Messages</h1>
- <div id="loading"><img src="css/images/loading.gif" alt="Loading..." /></div>
- <ul>
- <ul>
- </div>
- </div>
shoutbox.php
PHP Code
- <?php
- /************************
- CONSTANTS
- /************************/
- define("HOST", "localhost");
- define("USER", "root");
- define("PASSWORD", "");
- define("DB", "");
- /************************
- FUNCTIONS
- /************************/
- function connect($db, $user, $password){
- $link = @mysql_connect($db, $user, $password);
- mysql_query("set names utf8;");
- if (!$link)
- die("Could not connect: ".mysql_error());
- else{
- $db = mysql_select_db(DB);
- if(!$db)
- die("Could not select database: ".mysql_error());
- else return $link;
- }
- }
- function getContent($link, $num){
- $res = @mysql_query("SELECT * FROM content ORDER BY updatetime DESC LIMIT ".$num, $link);
- if(!$res)
- die("Error: ".mysql_error());
- else
- return $res;
- }
- function insertMessage($user, $message){
- $updatetime=date("Y-m-d H:i:s",time());
- $query = sprintf("INSERT INTO content (name, message,updatetime) VALUES('%s', '%s','$updatetime');", mysql_real_escape_string(strip_tags($user)), mysql_real_escape_string(strip_tags($message)));
- $res = @mysql_query($query);
- if(!$res)
- die("Error: ".mysql_error());
- else
- return $res;
- }
- /******************************
- MANAGE REQUESTS
- /******************************/
- if(!$_POST['action']){
- //We are redirecting people to our shoutbox page if they try to enter in our shoutbox.php
- header ("Location: index.html");
- }
- else{
- $link = connect(HOST, USER, PASSWORD);
- switch($_POST['action']){
- case "update":
- $res = getContent($link, 20);
- while($row = mysql_fetch_array($res)){
- $result .= "<li><strong>".$row['name']."</strong><img src=\"css/images/bullet.gif\" alt=\"-\" />".$row['message']." <span class=\"date\">".$row['updatetime']."</span></li>";
- }
- echo $result;
- break;
- case "insert":
- echo insertMessage($_POST['nick'], $_POST['message']);
- break;
- }
- mysql_close($link);
- }
- ?>
相关推荐
周公周金桥 2020-09-06
大象从不倒下 2020-07-31
AlisaClass 2020-07-19
MaureenChen 2020-04-21
xingguanghai 2020-03-13
teresalxm 2020-02-18
木四小哥 2013-05-14
SoShellon 2013-06-01
Simagle 2013-05-31
羽化大刀Chrome 2013-05-31
waterv 2020-01-08
LutosX 2013-07-29
vanturman 2013-06-27
wutongyuq 2013-04-12
luoqu 2013-04-10
today0 2020-09-22
89520292 2020-09-18
bigname 2020-08-25