我的js中的帖子不会工作

时间:2018-03-17 07:29:49

标签: javascript php ajax

  

JS:由godot修复!



/* Data Delete Starts Here */
$(".delete-file").click(function()
{
  var name = $(this).attr("name");
  var file_id = $(this).attr("id");
  var active_user = document.getElementById("username").value;
  if(confirm('Sure to Delete ' +name+ '?'))
  {
    $.ajax({
        url: 'suf-delete.php',
        type: 'post',
        async: false,
        data: {'file_id':file_id ,'active_user':active_user},
        success: function(response){
           $("#file_table"+file_id).fadeOut('slow');
        },
        error: function(error){
              console.log(error.responseText);
         //you could debug your php code if some error raises
         }

    });
  }
  return false;
});
/* Data Delete Ends Here */




  

PHP:工作正常(suf-delete.php)



elseif($_POST['file_id'] && $_POST['active_user'])
{
	$file_id = $_POST['file_id'];
	$active_user = $_POST['active_user'];
	$crud->filesDelete($file_id,$active_user);
}




我现在遇到了班级问题。

我认为这只是我的疑问。我试图使用下面提供的代码进行活动记录,删除,选择和取消链接项目。

请检查:

  

Class Crud



    public function filesDelete($file_id,$active_user)
    {
      $stmtFiles = $this->conn->prepare('SELECT * FROM tbl_files WHERE file_id=:file_id');
      $stmtFiles->execute(array(":file_id"=>$file_id));
      $unFile=$stmtFiles->fetch(PDO::FETCH_ASSOC);

      $userStmt = $this->conn->prepare('SELECT * FROM tbl_login WHERE username=:username');
      $userStmt->execute(array(":username"=>$active_user));
      $fetch = $userStmt->fetch(PDO::FETCH_ASSOC);

      $activity = "Deleted the file ".$unFile['file_name'];
      if($fetch['access_type']=="Design2K18ADMIN") {
        $type = "Administrator";
      }
      elseif ($fetch['access_type']=="Design2K18MANAGER") {
        $type = "Manager";
      }
      elseif ($fetch['access_type']=="Design2K18MODERATOR") {
        $type = "Moderator";
      }
      elseif ($fetch['access_type']=="Design2K18SIMPLE") {
        $type = "Simple";
      }

      $actLog = $this->conn->prepare("INSERT INTO activity_log(username, activity, type) VALUES(:username, :activity, :type)");
      $actLog->execute(array(":username"=>$active_user, ":activity"=>$activity, ":type"=>$type));

      $stmtSelFol = $this->conn->prepare('SELECT * FROM tbl_section WHERE sec_id=:sec_id');
      $stmtSelFol->execute(array(":sec_id"=>$unFile['sec_id']));
      $unFol=$stmtSelFol->fetch(PDO::FETCH_ASSOC);

      unlink("../Files/".$unFol['sec_folder']."/".$unFile['file_name']);

      $stmtDelFile = $this->conn->prepare('DELETE FROM tbl_files WHERE file_id=:file_id');
      $stmtDelFile->execute(array(":file_id"=>$file_id));

      return true;
    }




我通过在error.php中保存日志确认我的js和php工作正常

我将file_id和active_user的值保存在error.txt文件中。

出现9Daren,它是Item的值(id),当前会话用户是Daren。

请帮我查看我的公共功能为什么它什么都不做。

1 个答案:

答案 0 :(得分:2)

我会使用ajax

/* Data Delete Starts Here */
$(".delete-file").click(function()
{
  var name = $(this).attr("name");
  var file_id = $(this).attr("id");
  var active_user = document.getElementById("username").value;
  if(confirm('Sure to Delete ' +name+ '?'))
  {

    $.ajax({
        url: 'suf-delete.php',
        type: 'post',
        async: false,
        data: {'file_id':file_id ,'active_user':active_user},
        success: function(response){
           $("#file_table"+file_id).fadeOut('slow');
        },
        error: function(error){
              console.log(error.responseText);
         //you could debug your php code if some error raises
         }

    });
  }
  return false;
});
/* Data Delete Ends Here */