March 4, 2012

jquery comment system on.(…click) issue

Question by Anonymous

I’m having trouble keeping event handlers attached to future loaded selectors. For example, when pull.php is loaded, the confirmdelete is no longer hidden, and also the click event handlers no longer exist. I am new to jquery and ajax. Below is my code.
$id= (int)strip_tags($_GET[‘id’]);

$(document).ready(function() { //make a comment delete js file eventually, just so we can reference the source
    $('.deletecomment').on("click", function(e){
        var cid = $(this).attr("id");
        $('a#c'+cid).show(500, function(){ //right now as call back
            $(this).on("click", function(e){
            var id = $(this).attr("id"); 
            var did = id.substring(1);  


                   $("#commentarea").load("pull.php?id=<? echo $id; ?>");



                e.preventDefault();//so it doesn't interpret is as an anchor link



    e.preventDefault();//so it doesn't interpret is as an anchor link



the below script is the php part:

<div id="commentarea">


$query = mysql_query("SELECT users.user_id, users.username, users.miniavatar, comments.comment_id, comments.comment, comments.time_stamp FROM users INNER JOIN comments ON users.user_id=comments.products_users_user_id WHERE comments.products_products_id = '$id' ORDER BY comments.time_stamp DESC");

while($row2 = mysql_fetch_array($query)) {

<div id='singlecomment'>

<hr class="comment" />
<col width="*" />
<col width="400" />
<col width="*" />    
<td valign = "top" rowspan="2">
<a href="collection.php?profile=<? echo $row2['user_id']; ?>"><img src="<? echo $row2['miniavatar']; ?>" height="52" width="52" /></a> <br />
if ($user_id == $row2['user_id']) { 
    $cid = $row2['comment_id'];

    echo "<a id='$cid' class='deletecomment' title='Delete Post'>X</a> &nbsp";
    echo "<a id='c$cid' class='confirmdeletecomment'>confirm</a>";
<td valign="top">
<a class="blue" href="collection.php?profile=<? echo $row2['user_id']; ?>"> <? echo $row2['username']; ?> </a>
<span class="date"><? echo date("F j, Y g:i a ", strtotime($row2['time_stamp'])); ?> </span>
<td colspan="2">
<? echo stripslashes($row2['comment']); ?> <br/><br/>

Answer by Starx

I am guessing the event got messed up since you are the passing the event handler from

$('.deletecomment').on("click", function(e){


 $(this).on("click", function(e){

Change the event names, if you really want to handle them separately. More like updating your second handler as these will do goo

$(this).on("click", function(event){

Since I dont have your markup structure, I am guessing, when you are loading pull.php to #commentarea, another element with class confirmdelete should have been loaded as well, thus making the code execution incomplete logically.

Put, $(".confirmdelete").hide(); right above e.preventDefault() to see if I am right.

April 17, 2011

Ajax comments insert LI or replace UL?

Question by Aen Tan

I’m ajaxifying my comments feature and I’m choosing between 2 strategies. My comments list is an unordered list.

  1. Replace UL in DOM with UL from response.
  2. Insert last LI from response into UL in DOM.

The first option is simpler but I feel like it’s not the best or right way.
If I want to use the second option how do I know which comments in the response are newer than the ones in the DOM if more than one comment was posted at the same time?


Answer by JohnP

You can keep the timestamp as a variable for JS to refer.

When you load the page, your server side language will assign the timestamp of the last comment to be loaded to a javascript variable. From there on, everytime you load more comments, send the last timestamp you requested. Your server side language will check for comments that are newer than that timestamp. Make sure to update the timestamp as you send out more requests. Either by sending it as part of the response or keeping track of when you sent the request.

Answer by Starx

You should append the comment’s UL with latest LIs. Why? you do not have to grab all the full UL list and replace the old one. Reduce the changes that has to be made. Just grab the latest LI and append it at the last. LESS WORK MORE EFFICIENT

So far of your question, about how to know which one is the latest comment. Store timestamp as the comments are stored in the DB. While also fetching the comments for the first time, try to assign the variable holding the timestamp of the last item, then after search the database for everything that is greating the timestamp you stored, get the list and append them, then update the variable, with the latest time stamp again.

June 21, 2010

Limit posting to every 30 minutes

Question by jay

Thanks for looking.

I’m trying to write a script to set a cookie after the user posts a comment, limiting them from posting again for 30 minutes. I’m so confused on where to start. Please, could you guys help me out on how I do this?

I’m trying to use this jquery plugin – countdown timer.

Answer by Starx

Use Session

For example, after a post, put the current time + 30 minutes in your session line this
$_SESSION['postTimeFlag'] = time() + 1800;
Then whenever the user is about to post then the session

if(time()>$_SESSION['postTimeFlag']) { 
   //continue posting

Please fill the form - I will response as fast as I can!