php jquery 无刷新评论刷新后删除一个小时前的记录
php jquery 无刷新评论
演示
XML/HTML Code
<?php
define('INCLUDE_CHECK',1);
require "functions.php";
require "conn.php";
// remove tweets older than 1 hour to prevent spam
mysql_query("DELETE FROM add_delete_record WHERE id>1 AND updatetime<SUBTIME(NOW(),'0 1:0:0')");
//fetch the timeline
$q = mysql_query("SELECT * FROM add_delete_record ORDER BY ID DESC");
$timeline='';
while($row=mysql_fetch_assoc($q))
{
$timeline.=formatTweet($row['text'],$row['updatetime']);
}
// fetch the latest tweet
$lastTweet = '';
?>
<!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>php jquery 无刷新评论 www.freejs.net</title>
<link rel="stylesheet" type="text/css" href="demo.css" />
<script type="text/javascript" src="../../js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div id="twitter-container">
<form id="tweetForm" action="submit.php" method="post">
<span class="counter">140</span>
<label for="inputField">请留言测试</label>
<textarea name="inputField" id="inputField" tabindex="1" rows="2" cols="40"></textarea>
<input class="submitButton inact" name="submit" type="submit" value="提交" disabled="disabled" />
<div class="clear"></div>
</form>
<h3 class="timeline">Freejs.net</h3>
<ul class="statuses"><?php echo $timeline?></ul>
</div>
</body>
</html>
functions.php
PHP Code
<?php
if(!defined('INCLUDE_CHECK')) die('You are not allowed to execute this file directly');
function formatTweet($tweet,$updatetime)
{
$tweet=htmlspecialchars(stripslashes($tweet));
$flag = mt_rand(1,9);
return'
<li>
<a href="#"><img class="avatar" src="img/lito_s-ido_0'.$flag.'.png" width="48" height="48" /></a>
<div class="tweetTxt">
<strong><a href="#">demo</a></strong> '. preg_replace('/((?:http|https|ftp):////(?:[A-Z0-9][A-Z0-9_-]*(?:/.[A-Z0-9][A-Z0-9_-]*)+):?(/d+)?//?[^/s/"/']+)/i','<a href="$1" rel="nofollow" target="blank">$1</a>',$tweet).'
<div class="date">'.strtotime($updatetime).'</div>
</div>
<div class="clear"></div>
</li>';
}
?>
submit.php
PHP Code
<?php
define('INCLUDE_CHECK',1);
require "functions.php";
require "conn.php";
if(ini_get('magic_quotes_gpc'))
$_POST['inputField']=stripslashes($_POST['inputField']);
$_POST['inputField'] = mysql_real_escape_string(strip_tags($_POST['inputField']),$lr);
if(mb_strlen($_POST['inputField']) < 1 || mb_strlen($_POST['inputField'])>140)
die("0");
mysql_query("INSERT INTO add_delete_record SET text='".$_POST['inputField']."',updatetime=NOW()");
if(mysql_affected_rows($lr)!=1)
die("0");
echo formatTweet($_POST['inputField'],time());
?>