I have a stackoverflow style comment system where there are a variable number of posts (“answers”) on a page that people can comment on. I am trying to use jquery to grab a unique selector of a users comment, submit it into a mysql database and display it, all without refreshing. The trouble is that i don’t know how to select an individual comment, because the comment’s need to have a unique selector and right now they are all under the same class (.commentBox).
JQUERY:
<script type='text/javascript'>
$('document').ready(function(){
$('.submitCommentBox').click(function(){
var comment = $(' //idk ').valu();
var questionid = $(' //idk ').val();
var answerid=$(' //idk ').val();
$.post('../comment.php',
{
comment: comment,
questionid: questionid,
answerid: answerid,
},
function(response){
$('#commentContainer').load('../writecomment.php');
});
}):
});
</script>
HTML (this is in a while loop and echos multiple times depending on number of posts):
<div class='answerContainer'>
<p name='singleAnswer' value='$answerid[$f]'>$answer[$f]</p>
<p>$answeruser[$f]</p>
<p> $time[$f] seconds</p>
<p> $answerrating[$f]</p>
<form id='form$c'>
<div id='commentContainer'></div>
<textarea class='commentBox' placeholder='...comment' wrap='soft' name='comment' value='$c'></textarea>
<input type='button' value='submit' class='submitCommentBox'>
</form>
</div>
Instead of the wire up on the submit you can do it on the form and just change the submit button to type=”submit”.
I’d refer to the jquery api for this for more details. http://api.jquery.com/jQuery.post/