我有这个评论系统效果不佳。它将注释插入数据库,但仅在刷新页面后显示。我希望它在点击提交按钮后立即显示,并以滑动效果显示。
我怀疑$arr[$k] = mysql_real_escape_string($v);
导致问题。我现在正在使用PDO,所以我知道一定不能存在,但当我将其删除后,代码就会停止工作。
我的代码如下。
<?php
class Comment{
private $data = array();
public function __construct($row){
$this->data = $row;
}
public function markup(){
$d = &$this->data;
$link_open = '';
$link_close = '';
if($d['url']){
$link_open = '<a href="'.$d['url'].'">';
$link_close = '</a>';
}
$d['dt'] = strtotime($d['dt']);
$url = 'http://'.dirname($_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]);
return '
<div class="comment">
<div class="name">'.$link_open.$d['name'].$link_close.'</div>
<div class="date" title="Added at '.date('H:i \o\n d M Y',$d['dt']).'">'.date('d M Y',$d['dt']).'</div>
<p>'.$d['body'].'</p>
</div>
';
}
public static function validate(&$arr){
$errors = array();
$data = array();
if(!($data['email'] = filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL))){
$errors['email'] = '<div>Please insert your email</div>';
}
if(!($data['url'] = filter_input(INPUT_POST,'url',FILTER_VALIDATE_URL))){
$url = '';
}
if(!($data['body'] = filter_input(INPUT_POST,'body',FILTER_CALLBACK,array('options'=>'Comment::validate_text')))){
$errors['body'] = '<div>Please type your comment</div>';
}
if(!($data['name'] = filter_input(INPUT_POST,'name',FILTER_CALLBACK,array('options'=>'Comment::validate_text')))){
$errors['name'] = '<div>Please insert your name</div>';
}
if(!empty($errors)){
$arr = $errors;
return false;
}
foreach($data as $k=>$v){
$arr[$k] = mysql_real_escape_string($v);
}
$arr['email'] = strtolower(trim($arr['email']));
return true;
}
private static function validate_text($str){
if(mb_strlen($str,'utf8')<1)
return false;
$str = nl2br(htmlspecialchars($str));
$str = str_replace(array(chr(10),chr(13)),'',$str);
return $str;
}
}
?>
还有我的js文件:
<script>
$(document).ready(function(){
var working = false;
$('#addCommentForm').submit(function(e){
e.preventDefault();
if(working) return false;
working = true;
$('#submit').val('working');
$('span.error').remove();
$.post('commenting/submit.php',$(this).serialize(),function(msg){
working = false;
$('#submit').val('submit');
if(msg.status){
$(msg.html).hide().insertBefore('#addCommentContainer').slideDown();
$('#body').val('');
}else {
$.each(msg.errors,function(k,v){
$('label[for='+k+']').append('<span class="error">'+v+'</span>');
});
}
},'json');
});
});
</script>
答案 0 :(得分:0)
上面的标签是什么:
<div class="comment">
<div class="name">'.$link_open.$d['name'].$link_close.'</div>
<div class="date" title="Added at '.date('H:i \o\n d M Y',$d['dt']).'">'.date('d M Y',$d['dt']).'</div>
<p>'.$d['body'].'</p>
</div>
如果您在commets标记内显示您的评论,例如:
<div id="comments">
<div class="comment">
<div class="name">'.$link_open.$d['name'].$link_close.'</div>
<div class="date" title="Added at '.date('H:i \o\n d M Y',$d['dt']).'">'.date('d M Y',$d['dt']).'</div>
<p>'.$d['body'].'</p>
</div>
</div>
然后编辑你的js如下:
<script>
$(document).ready(function(){
var working = false;
$('#addCommentForm').submit(function(e){
e.preventDefault();
if(working) return false;
working = true;
$('#submit').val('working');
$('span.error').remove();
$.post('commenting/submit.php',$(this).serialize(),function(msg){
working = false;
$('#submit').val('submit');
if(msg.status){
$(msg.html).hide().insertBefore('#addCommentContainer').slideDown();
$('.comment').val('');
/// I added below code to show updated comments inside of comments tag
$('#comments').html(msg.html+$('#comments').html());
}else {
$.each(msg.errors,function(k,v){
$('label[for='+k+']').append('<span class="error">'+v+'</span>');
});
}
},'json');
});
});
</script>