普通网友 2020-01-28 11:37
浏览 14

Ajax响应未返回

I am having a table with data where I have an action button in last column to delete that particular row. I want to make the delete via ajax and without refreshing the page. I am using the following code but their is no response coming from the ajax page. Also the queries at the ajax page is not executing. Can I have some insight over what could be possibly wrong.

      <a href="#" class="btnIcon    glyphicon glyphicon-trash delbutton" data-toggle="tooltip" title="Delete" id="<?php echo $id; ?>"></a>

 <script type="text/javascript" >
        $(function() {

            $(".delbutton").click(function() {
                var del_id = $(this).attr("id");
                var info = 'id=' + del_id;
                var $tr = $(this).closest('tr');

                if (confirm("Sure you want to delete this post? This cannot be undone later.")) {

                    $.ajax({
                        type : "POST",
                        url : "delete_entry.php", //URL to the delete php script
                        data: info,
                    success : function(response) {
                               if(response=='deletion success'){
                                $tr.find('td').fadeOut(1000,function(){ $tr.remove();  }); 

                               }
                        }
                    });  
                }
                return false;
            });
        });
 </script>

And at delete_entry.php

 <?php
header('Content-Type: application/json');  

session_start();

require("../config.php"); 
require("../Database.class.php"); 
require("../site.php"); 
$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);     
$fnc=new site_functions($db);

    $id     =   $_POST['id'];

    $deleted_date = date("Y-m-d h:i:s");
  $deleted_by       =    $_SESSION['session_admin_id'] ;

  $nots = $db->idToField("tbl_ques","notes",$id);
   if ($nots == "")
   {
   $date_string = "last deleted on|".$deleted_date."|" ;
   }
   else {
   $date_string = $nots."last deleted on|".$deleted_date."|" ;
   }
     $fnc->update_is_not_deleted_for_Pearsonvue("tbl_ques",$id, "$deleted_date", $deleted_by);       
    $notes_data = array("notes"=>$date_string);

  if($db->query_update("tbl_ques", $notes_data, "id=$id")){
      http_response_code();        
      echo json_encode('deletion success');
    }else{
       http_response_code(204);       
    }

?>
  • 写回答

1条回答 默认 最新

  • weixin_33698043 2020-01-28 14:25
    关注

    Change your java script function as below

    <script type="text/javascript" >
            $(function() {
    
                $(".delbutton").click(function() {
                    var del_id = $(this).attr("id");
                    var info = 'id=' + del_id;
                    if (confirm("Sure you want to delete this post? This cannot be undone later.")) {
    
                        $.ajax({
                            type : "POST",
                            url : "delete_entry.php", //URL to the delete php script
                            data: info,
               //changes from 
                           success : function(response) {
                                   if(response=='deletion success'){  
                               $(this).parents(".record").animate("fast").animate({
                                     opacity : "hide"
                               }, "slow");
                                   }
                            }
                        });  
                //changes to                  
                    }
                    return false;
                });
            });
      </script>
    

    Also change you php file

    <?php
    
    header('Content-Type: application/json');  // Add this line its must
    
    session_start();
    
     $db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);     
    
    
        $id     =   $_POST['id'];
    
        $deleted_date = date("Y-m-d h:i:s");
      $deleted_by       =    $_SESSION['session_admin_id'] ;
    
      $nots = $db->idToField("tbl_question","notes",$id);
       if ($nots == "")
       {
       $date_string = "last deleted on|".$deleted_date."|" ;
       }
       else {
       $date_string = $nots."last deleted on|".$deleted_date."|" ;
       }
    
          $fnc->update_is_not_deleted_for_Pearsonvue("tbl_question",$id, "$deleted_date", $deleted_by);       
        $notes_data = array("notes"=>$date_string);
    //changes from
        if($db->query_update("tbl_question", $notes_data, "id=$id")){
          http_response_code();        
          echo json_encode('deletion success');
        }else{
           http_response_code(204);       
        }
    //changes to
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制