I’m building a comment system modeled after Tumblr’s, where each post will have it’s own comments. I’ve modded a system from 9lessons and am having an issue implementing it for multiple instances on a page. Each post has its own ID, so I’m trying to set this up using the post’s ID. It works fine for a single post, but when there’s more than one on a page it doesn’t work.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="noblecount/jquery.NobleCount.js"></script>
<script type="text/javascript">
$(function() {
$('.submit').click(function() {
var postID = $('#postID').val();
var comment = $('#com_comment'+postID).val();
var dataString = 'com_comment=' + comment + '&postID=' + postID;
if(comment=='') {
alert('Please enter a comment');
} else {
$('#flash'+postID).show();
$('#flash'+postID).fadeIn(400).html('<img src="/images/ajax-loader.gif" align="absmiddle"> <span class="loading">Loading Comment...</span>');
$.ajax({
type: 'POST',
url: 'comments_ajax.php',
data: dataString,
cache: false,
success: function(html){
$('ol#update'+postID).append(html);
$('ol#update'+postID+'li:last').fadeIn('slow');
document.getElementById('com_comment'+postID).value='';
$('#flash'+postID).hide();
}
});
}
return false;
});
});
$(document).ready(function(num) {
$('#com_comment'+num).NobleCount('#replyCount'+num,{max_chars: 255,on_negative: 'go_red',on_positive: 'go_green',block_negative: true});
});
function ShowHide(num){
$('#comment'+num).animate({'height':'toggle'});
}
</script>
</head>
<body>
<div class="comments">
<?php $num = 1; ?>
<a onclick="ShowHide(<?php echo $num; ?>); return false;" href="#">reply</a>
<div id="comment<?php echo $num; ?>">
<ol id="update<?php echo $num; ?>" class="timeline">
<?php
//$post_id value comes from the POSTS table
$sql = mysql_query("SELECT * FROM posts_comments WHERE com_post_id = 1");
while($row = mysql_fetch_array($sql)) {
echo '<li class="box"><img src="/" border="0" alt="" width="16" height="16" class="avi16" /> '.$row['com_comment'].'</li>';
}
?>
</ol>
<div id="flash<?php echo $num; ?>" align="left"><!-- loader --></div>
<form action="#" method="post">
<input type="hidden" name="postID" id="postID" value="<?php echo $num; ?>" />
<textarea name="com_comment<?php echo $num; ?>" id="com_comment<?php echo $num; ?>" class="taReply"></textarea><br />
<input type="submit" value="Reply" class="submit buttonReply" /> <div class="divCount"><span id="replyCount<?php echo $num; ?>">255</span> max</div>
</form>
</div>
</div>
</body>
</html>