php使用ajax

时间:2019-02-28 05:43:25

标签: javascript php ajax comments delete-row

我将尝试删除评论回复,其中有     parent_id = 0

已修复。我可以删除父注释,但是当我删除子注释(回复)时,父注释已删除,但子注释未删除。并且除非我刷新,否则答复注释仍保留在数据库中并显示在页面中它。 我有获取评论和回复这样的功能

        <?php
    ini_set('memory_limit', '1024M');
     require_once __DIR__.'/../vendor/autoload.php';
     include __DIR__.'/../lib/Database.php';
     include __DIR__.'/../config/config.php';
     include __DIR__.'/../lib/Session.php';
     include __DIR__.'/../helpers/Format.php';
     $db = new Database();
     $fm = new Format();
    ?>
        <?php
             $commentQuery = "SELECT * FROM `comment` WHERE `parent_id`= '0' 
ORDER BY `id` DESC";
             $result = $db->select($commentQuery);
             $commentReply='';
            if($result) {
               while ($cmResult = $result->fetch_assoc()) {
          ?>
           <?php
           $commentReply .= ' 
           <div class="panel-wrapper parent_wrapper" id = 
"'.$cmResult['id'].'">
           <div class="panel panel-default">
        <div class="fa-2x text-right" style="border: none;">    
          <button class=" btn bn-default delClick">             
      <span class="fa-layers fa-fw" style="background:Mistyrose; border: 
none;">
        <i class="fa-inverse fas fa-times-circle" style="color: black;" 
data-fa-transform="shrink-3">
    </i>
      </span>
      </button>
      </div>                
        <div class="panel-heading">

                               <h4>
                                <span class= "fa-layers fa-fw">
                                <i class="fa-inverse fas fa-user-graduate fa-3x" style=" background:MistyRose">                         
    </i> </span>
      ' . $cmResult["sender"] . '                   </h4>                                                             
    <p class="date">' . $fm->DateFormat($cmResult["date"]) . '</p>
      </div> 
      <div class="panel-body">              
      <p class="comment">'.$fm->textShorten($cmResult["comment"],70).'</p>
         <button type="button"  class="btn btn-warning reply" id="' . $cmResult["id"] . '">REPLY</button>
         </div>                        
         </div>
         </div>';
        ?>
    <?php
        $commentReply .= getReply($db,$fm,$cmResult['id']);
            }
            echo $commentReply;
        }
    ?>
    <?php
     function getReply($db,$fm,$parentId=0,$margin = 0)
     {
             //$fm = new  Format();
             //$db = new Database();
             $commentReply = '';
             $getQuery = "SELECT * FROM `comment` WHERE `parent_id`='" . $parentId . "' ORDER BY `id` DESC";
             if( $result = $db->select($getQuery))
             {
              $count = mysqli_num_rows($result);

         if($parentId== 0)
         {
             $margin = 0;
         }else
         {
             $margin = $margin + 28;
         }
         if(!empty($count)||$count>0)
         {
            while( $cmResult = $result->fetch_assoc())
               {
                   //$reply = 1;
        // $fm =new Format();
        $cmDate = $fm->DateFormat($cmResult['date']);
         $shorten = $fm->textShorten($cmResult['comment'],70);
         $commentReply.='
         <div class="panel-wrapper" style="margin-left: '. $margin. 'px" id = "'.$cmResult['id'].'">
          <div class="panel panel-default">
          <div class="fa-2x text-right" style="border: none;">  
          <button class=" btn btn-default delClick">                
      <span class="fa-layers fa-fw" style="background:Mistyrose; border: none;">
        <i class="fa-inverse fas fa-times-circle" style="color: black;" data-fa-transform="shrink-3">
    </i>
      </span>
      </button>
      </div>
         <div class="panel-heading">        
         <h4>
             <span class="fa-layers fa-fw">
                <i class="fa-inverse fas fa-user-graduate fa-3x" data-fa-transform="shrink-8"
                    style="background:MistyRose">
                         </i>
                            </span>                         
                            '. $cmResult["sender"].'                          
                             </h4>
                             <p class="date">'.$cmDate.'</p>
                         </div>
                         <div class="panel-body">
                             <p class="comment">'. $shorten.'</p>
                             <button type="button" class="btn btn-warning reply" id="'. $cmResult["id"].'">
                                 REPLY
                             </button>
                         </div>
                     </div>
                 </div>';
                 $commentReply .= getReply($db,$fm,$cmResult['id'],$margin);
             }
             }}
         return $commentReply;
     }
    ?>

我尝试使用ajax删除回复评论

$(document).on('click','.delClick',function (e) {
e.preventDefault(); 
        var element = $(this).parent().parent();
    var del_id = $(this).attr('id');
$.ajax({
    url:"../scripts/disqus_template.php",
    type: "POST",
    data: "id=" + del_id,
    dataType: "JSON",
    cache: false,
    success: function (response){          
        if(response.error) {             
            $('#delmessage').fadeIn('500').html(response.message);
            $('#delmessage').fadeOut('500').html(response.message);
            $('#delmessage').fadeIn('500').html(response.message);                      
            $('#delmessage').fadeOut('500').html(response.message);                      
            //$('#delmessage').fadeIn('500').html(response.message);                      
            //$('#delmessage').fadeOut('500').html(response.message);  
        element.slideUp('slow', function() {$(this).remove();});
    if($("#countNumber").length > 0) {
                    var currentCount = parseInt($("#countNumber").text());
                    var newCount = currentCount - 1;
                    $("#countNumber").text(newCount)
                }               
        }       
    }

    });
    return false;

   // return false;
});

用于删除像这样的注释的php脚本->

<?php
require_once __DIR__.'/../vendor/autoload.php';
include __DIR__.'/../lib/Session.php';
//include __DIR__.'/../scripts/disqus.php';
include __DIR__.'/../config/config.php';
include __DIR__.'/../lib/Database.php';
//Session:: init();
$db = new Database();

//var_dump($id);
//die();

 $commentQuery = "SELECT * FROM `comment` WHERE `comment`.`parent_id`= 0";
 if(     $result = $db->select($commentQuery))
 {

    $delResult = mysqli_num_rows( $result);
    if(!empty($delResult)|| $delResult>0)
    {
    if($cmResult =$result ->fetch_assoc())
    {
    //$reply= 1;
    $id = $cmResult['id'];
    $parentId = $cmResult['parent_id'];

    if($id)
    {
    $delete = "DELETE  FROM comment WHERE `comment`.`id`='".$id."'";
    $result= $db->delete($delete);

    if($result)
    {
     $msg = "<p class='alert alert-success'>.<span class='fa-layers fa-fw'>. 
   <i class='fas fa-check-circle fa-2x'></i></span>success</p>";
    $status = array
    (
        'error' => 3,
        'message' => $msg
    );

    } else {
    $msg = "<p class='alert alert-danger'>.<i class='fas fa-times-circle fa- 
   2x'>.</i>deny</p>";
    $status = array
    (
        'error' => 4,
        'message' => $msg
    );
    }       

    }
     echo json_encode($status); 

    }

    }       
    }

但是那不走运。我想我没有得到一个简单的东西,眨眼之间。

0 个答案:

没有答案