reply for comments form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rian92
    New Member
    • Oct 2014
    • 5

    reply for comments form

    Hai, I'm new in php and I have a problem when create reply for comments form. I try to create comments form that has reply button but every time I click reply button it not working at all. Hope someone can help me. Here is my code for comments form.
    Code:
    <?php
    include("db_connect.php");
    include("functions.php");
    ?>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <script type='text/javascript' src='jquery.pack.js'></script>
    <script type='text/javascript'>
    $(function(){
     $("a.reply").click(function() {
      var id = $(this).attr("id");
      $("#parent_id").attr("value", id);
     });
    });
    </script>
    
    <title>Threaded Comments</title>
    </head>
    <body>
    <div id='wrapper'>
      <ul>
        <?php
    $q = "SELECT * FROM comment WHERE parent_id = 0";
    $r = mysql_query($q);
    while($row = mysql_fetch_assoc($r)):
     getComments($row);
    endwhile;
    ?>
      </ul>
      <form id="comment_form" action="post_comment.php" method='post'>
    	<label for="name">Name:</label>
    	<input type="text" name="name" id='name'/>
    	<label for="comment_body">Comment:</label>
    	<textarea name="comment_body" id='comment_body'></textarea>
    	<input type='hidden' name='parent_id' id='parent_id' value='0'/>
    	<div id='submit_button'>
    		<input type="submit" value="Add comment"/>
    	</div>
    </form>
    Code:
    <?php
    function getComments($row) {
     echo "<li class='comment'>";
     echo "<div class='name'>".$row['name']."</div>";
     echo "<div class='comment_body'>".$row['comment']."</div>";
     echo "<div class='timestamp'>".$row['created_at']."</div>";
     echo "<a href='#comment_form' class='reply' id='".$row['id']."'>Reply</a>";
     /* The following sql checks whether there's any reply for the comment */
     $q = "SELECT * FROM comment WHERE parent_id = ".$row['id']."";
     $r = mysql_query($q);
     if(mysql_num_rows($r)>0) // there is at least reply
      {
      echo "<ul>";
      while($row = mysql_fetch_assoc($r)) {
       getComments($row);
      }
      echo "</ul>";
      }
     echo "</li>";
    }
    ?>
    Code:
    <?php
    include("db_connect.php");
    $name = mysql_real_escape_string($_POST['name']);
    $comment_body = mysql_real_escape_string($_POST['comment_body']);
    $parent_id = mysql_real_escape_string($_POST['parent_id']);
    
    $q = "INSERT INTO comment (name, comment, parent_id) VALUES ('$name', '$comment_body', '$parent_id')";
    $r = mysql_query($q);
    if(mysql_affected_rows()==1) {
    	header("location: index.php");
    }
    else {
    echo "Comment cannot be posted. Please try again.";
    }
    ?>
    Thank you in advance.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I try to create comments form that has reply button but every time I click reply button it not working at all.
    1) you don’t have a reply button (you have a reply link which takes you to the form, but it may be styled as a button though)
    2) your form’s button says 'add comment'
    3) what do you expect to happen
    4) what happens instead

    and finally, the mysql functions are deprecated. use the more modern PDO or MySQLi extensions instead.

    Comment

    Working...