这是我的index.html:
<div style="margin:50px">
<a href="#" class="like" id="1" name="up"><img src="likebig.png" alt="Like" border="0"></a>
<a href="#" class="like" id="1" name="down"><img src="dislike.png" alt="dislike" border="0"></a>
<div id="votebox">
<span id='close'><a href="#" class="close" title="Close This">X</a></span>
<div style="height:13px">
<div id="flash">Loading........</div>
</div>
<div id="content"></div>
</div>
</div>
和rating.php:
include("db.php");
if($_POST['id'] > 0 && $_POST['name']> 0){
$id=mysql_escape_String($_POST['id']);
$name=mysql_escape_String($_POST['name']);
mysql_query("update messages set $name=$name+1 where id='$id'");
$result=mysql_query("select up,down from messages where id='$id'");
$row=mysql_fetch_array($result);
$up_value=$row['up'];
$down_value=$row['down'];
$total=$up_value+$down_value;
$up_per=($up_value*100)/$total;
$down_per=($down_value*100)/$total;
?>
<div style="margin-bottom:10px">
<b>Ratings for this blog</b> ( <?php echo $total; ?> total)
</div>
<table width="700px">
<tr>
<td width="30px">
<img src="likeup.png">
</td>
<td width="60px"><?php echo $up_value; ?></td>
<td width="600px"><div id="greebar" style="width:<?php echo $up_per; ?>%"></div></td>
</tr>
<tr>
<td width="30px">
<img src="likedown.png">
</td>
<td width="60px"><?php echo $down_value; ?></td>
<td width="600px"><div id="redbar" style="width:<?php echo $down_per; ?>%"></div></td>
</tr>
</table>
<?php
}
这是一个ajax脚本:
<script type="text/javascript">
$(document).ready(function() {
$(".like").click( function() {
var id=$(this).attr("id");
var name=$(this).attr("name");
var dataString = 'id='+ id + '&name='+ name;
$("#votebox").slideDown("slow");
$("#flash").fadeIn("slow");
$.ajax({
type: "POST",
url: "rating.php",
data: dataString,
cache: false,
success: function(html){
$("#flash").fadeOut("slow");
$("#content").html(html);
}
});
});
$(".close").click(function() {
$("#votebox").slideUp("slow");
});
});
</script>
我已经尝试了很长时间,但我无法修复错误。
单击相似或不同按钮后,它没有显示计数器编号和计数条。
现在它显示为http://s15.postimg.org/e6pvjkrej/Untitled_1.png。
我需要这样:http://s15.postimg.org/qmmldbkqj/Untitled_2.png
我该如何解决这个问题?任何人都可以帮我解决这个问题吗?
提前感谢。
答案 0 :(得分:0)
不仅是“绿栏”和“红栏”。问题也在于输出。你需要回显所有的html,以便ajax可以得到html。
<?php
include("db.php");
if($_POST['id'] > 0 && $_POST['name']> 0){
$id=mysql_escape_String($_POST['id']);
$name=mysql_escape_String($_POST['name']);
mysql_query("update messages set $name='".$name+1."' where id='".$id."'");
$result=mysql_query("select up,down from messages where id='".$id."'");
$row=mysql_fetch_array($result);
$up_value=$row['up'];
$down_value=$row['down'];
$total=$up_value+$down_value;
$up_per=($up_value*100)/$total;
$down_per=($down_value*100)/$total;
//HTML Output for AJAX Calls
echo "<div style='margin-bottom:10px'>
<b>Ratings for this blog</b> (". $total ." total)
</div>
<table width='700px'>
<tr>
<td width='30px'>
<img src='likeup.png'>
</td>
<td width='60px'>". $up_value ."</td>
<td width='600px'><div id='greebar' style='width:'". $up_per ."%'></div></td>
</tr>
<tr>
<td width='30px'>
<img src='likedown.png'>
</td>
<td width='60px'>".$down_value."</td>
<td width='600px'><div id='redbar' style='width:'". $down_per ."%'></div></td>
</tr>
</table>";
}
?>
以下是教程中HTML输出的代码供参考:
echo '<b>Ratings for this blog</b> ( '.$total.' total)
Like :'.$up_value.'
<div id="greebar" style="width:'.$up_per.'%"></div>
Dislike:'.$down_value.'
<div id="redbar" style="width:'.$down_per.'%"></div>';