Sjmon
Sjmon

Reputation: 553

jquery delete selected comment

I have made a forum and I want the user to delete their comment if they don't want it.

Now it deletes the first post and not the post that they have clicked "delete" on. How can I check that its the comment they click on that gets deleted?

I have made this:

$(function() {
    $(".slet").click( function(event) {
        event.preventDefault();
        //$(this).css("background-color","#000");
        var id = "<?=$deleteID?>";
        //alert(id);
        $("#forum_content").fadeOut();
    });
});

and my php: it is a little messy.

        <?php foreach ($comments as $comment): ?>


        <?php
            if($comment->level == "admin")
            { 
                echo '<div class="adminclass">';
            } 
        ?>

            <div id="forum_content" class="content">

            <div class="forum_comment">

        <div class="forum_profil_img"><img width="90" height="90" src="<?php echo base_url();?>images/users/thumbs/<?php echo $query->profile_picture; ?>"></div><!-- forum_profil_img -->
            <div class="forum_post_content">

<span class="post_navn"><?php echo anchor('profil/index/'.$comment->kommentar_brugernavn, $comment->kommentar_brugernavn); ?></span>
                <span style="font-size:11px; margin-left:3px; color:#686868;"><i> Siger</i></span><br>
                <div id="data"><?php echo nl2br($comment->indhold); ?></div><!-- data -->

            </div><!-- forum_post_content -->
            <div style="width:auto; float:right; color:#a0a0a0; clear:left; position:relative; bottom:-15px;" id="forum_dato">
                <i>
                <abbr class="timeago" title="<?php echo $comment->dato; ?>"><?php echo $comment->dato; ?></abbr>    
                <?php
                if($this->session->userdata('logget_ind') == 1 && 
                   $this->session->userdata('username') == $comment->brugernavn &&
                   time() - $comment->time < 300)
               { echo "- <a href=''>Ret</a> - <a class='slet' href=''>Slet</a>";
                 $deleteID = $comment->commentID;
                } ?>


                </i>
            </div><!-- forum_post_content -->
            </div><!-- forum_comment -->

            <?php if ($comment->level == "admin") { ?> <span class="admin-ribbon"></span></div> <?php } ?>
            </div><!-- content -->

        <?php endforeach ?> 

Upvotes: 0

Views: 413

Answers (2)

S..
S..

Reputation: 5758

On each page use a counter, on which you assign a unique id to each comment from as it's added. That way you can delete comments irrelevant of their order (original or ordered).

Upvotes: 1

Chandu
Chandu

Reputation: 82963

Change the line echo "- <a href=''>Ret</a> - <a class='slet' href=''>Slet</a>" to

echo "- <a href=''>Ret</a> - <a class='slet' id='slet-".$comment->commentID."' href=''>Slet</a>"

and the click handler to:

$(function() {      
    $(".slet").click( function(event) {          
        event.preventDefault();          
        //$(this).css("background-color","#000");          
        var id = $(this).attr("id").replace(/[^0-9]/g, "");
        alert(id);          
        $("#forum_content").fadeOut();      
    });  
}); 

Upvotes: 0

Related Questions