普通网友 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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀