首页 > 解决方案 > 想在我的评论系统回复部分添加一些编辑/删除按钮

问题描述

索引.PHP

    <body>
      <br />
      <h2 align="center"><a href="#">Comment System using PHP and Ajax</a></h2>
      <br />
      <div class="container">
       <form method="POST" id="comment_form" > 
        <div class="form-group w-50">
         <input type="text" name="comment_name" id="comment_name" class="form-control" value="<?php echo $_SESSION['username'];?>" readonly/>
        </div>
        <div class="form-group">
         <textarea name="comment_content" id="comment_content" class="form-control" placeholder="Enter Comment" rows="5"></textarea>
        </div>
        <div class="form-inline ">
        <div class="form-group">
         <input type="hidden" name="comment_id" id="comment_id" value="0" />
         <input type="submit" name="submit" id="submit" class="btn btn-info" value="Comment" />
        </div>
        <input type="reset" value="Cancel" class="btn btn-primary" style="margin-left: 5px;">
        </div>

       </form>
       <span id="comment_message"></span>
       <br />
       <div id="display_comment"></div>
      </div>
     </body>
    </html>

    <script>


    $(document).ready(function(){

     $('#comment_form').on('submit', function(event){
      event.preventDefault();
      var form_data = $(this).serialize();//CONVERTS/STORES THE FORM INTO URL IN CODE
      $.ajax({
       url:"add_comment.php",//WHERE WILL THE INFO GO ---- OR ---- SENDS REQUEST/GOES TO ADD_COMMENT.PHP and records the info
       method:"POST",//FOUND IN THE FORM METHOD, telss what kind of method to use to send the data to server so we use ---POST---
       data:form_data,//data: it is the variable/code we used to convert/store the form into url in code
       dataType:"JSON",
       success:function(data)//Will be called if the sending of data to the comment.php is successful 
       {                    //and success will receive the same data/the form_data from server OR DATA WAS RECEIVED BY SUCCESS

        if(data.error != '')//check if there is no error in the process and DISPLAYS THE DATA BELOW
        {
         $('#comment_form')[0].reset();//as Array, it starts with 0 and the .reset() JUST RESETS THE FORM FIELD/OFF THE AUTO COMPLETE   
         $('#comment_message').html(data.error);//outputs if there is an error or not
         $('#comment_id').val('0');
         load_comment();
        }
       }
      })
     });

     load_comment();

     function load_comment()//loads all the comment
     {
      $.ajax({
       url:"fetch_comment.php",
       method:"POST",
       success:function(data)//DISPLAYS DATA BELOW if success
       {
        $('#display_comment').html(data);
       }
      })
     }

     $(document).on('click', '.reply', function(){
      var comment_id = $(this).attr("id");
      $('#comment_id').val(comment_id);
      $('#comment_name').focus();// if click it will focus on the comment name
     });



});
</script>

ADD_COMMENT.php

<?php

    session_start();

    //add_comment.php

    $connect = new PDO('mysql:host=localhost;dbname=testing', 'root', '');

    $error = '';
    $comment_name = $_SESSION['username'];
    $comment_content = '';

    if(empty($_POST["comment_name"]))
    {
     $error .= '<p class="text-danger">Name is required</p>';
    }
    else
    {
     $comment_name = $_POST["comment_name"];
    }

    if(empty($_POST["comment_content"]))
    {
     $error .= '<p class="text-danger">Comment is required</p>';
    }
    else
    {
     $comment_content = $_POST["comment_content"];
    }

    if($error == '')//if this condition runs then there is no validation error or whatsoever
    {
     $query = "
     INSERT INTO tbl_comment 
     (parent_comment_id, comment, comment_sender_name) 
     VALUES (:parent_comment_id, :comment, :comment_sender_name)
     ";
     $statement = $connect->prepare($query);
     $statement->execute(
      array(
       ':parent_comment_id' => $_POST["comment_id"],
       ':comment'    => $comment_content,
       ':comment_sender_name' => $comment_name
      )
     );
     $error = '<label class="text-success">Comment Added</label>';
    }

    $data = array(
     'error'  => $error
    );

    echo json_encode($data);

    ?>

FETCH_COMMENT.php

回复部分在哪里

    <?php
    session_start();
    //fetch_comment.php

    $connect = new PDO('mysql:host=localhost;dbname=testing', 'root', '');
    $query = "
    SELECT * FROM tbl_comment 
    WHERE parent_comment_id = '0' 
    ORDER BY comment_id DESC
    ";

    $statement = $connect->prepare($query);

    $statement->execute();

    $result = $statement->fetchAll();
    $output = '';
    $delete = '';
    foreach($result as $row)
    {
      $output .= '
     <div class="panel panel-default">
      <div class="panel-heading">By <b>'.$row["comment_sender_name"].'</b> on <i>'.$row["date"].'</i></div>
      <div class="panel-body">'.$row["comment"].'</div>
      <div class="panel-footer" align="right"><button type="button" class="btn btn-default reply" id="'.$row["comment_id"].'">Reply</button></div>';

     $output .= get_reply_comment($connect, $row["comment_id"]);
    }

    echo $output;



    function get_reply_comment($connect, $parent_id = 0, $marginleft = 0)
    {
     $query = "
     SELECT * FROM tbl_comment WHERE parent_comment_id = '".$parent_id."'
     ";
     $output = '';
     $statement = $connect->prepare($query);
     $statement->execute();
     $result = $statement->fetchAll();
     $count = $statement->rowCount();
     if($parent_id == 0)
     {
      $marginleft = 0;
     }
     else
     {
      $marginleft = $marginleft + 48;
     }
     if($count > 0)
     {
      foreach($result as $row)
      {
       $output .= '
       <div class="panel panel-default" style="margin-left:'.$marginleft.'px">
        <div class="panel-heading">By <b>'.$row["comment_sender_name"].'</b> on <i>'.$row["date"].'</i></div>
        <div class="panel-body">'.$row["comment"].'</div>
        <div class="panel-footer" align="right"><button type="button" class="btn btn-default reply" id="'.$row["comment_id"].'">Reply</button></div>
       </div>
       ';
       $output .= get_reply_comment($connect, $row["comment_id"], $marginleft);
      }
     }
     return $output;
    }



    ?>

标签: phpajax

解决方案


推荐阅读