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);??
- }??
- ??
- ??
- ?>??
?
原文地址:http://www.freejs.net/article_biaodan_125.html