2016-02-19 56 views
2

嗨我想运行一个删除脚本,它将使用ajax和php从我的数据库中删除记录。在没有ajax javascript文件的情况下,delete_file.php脚本可以正常工作并从数据库中删除(所以我在assix中问题在于javascript文件的某处)。一旦我添加了javascript文件,它只会运行,而delete_file.php不会。基本上我通过三个ID通过delete_file.php找到他们在他们各自的表中,然后使用这些变量找到正确的文件删除。任何帮助是极大的赞赏。我需要一个新鲜的一双眼睛,感谢使用PHP,Ajax从数据库中删除

什么我点击 [![请在此输入图像说明] [1] [1]

HTML

<?php echo'<li class="col student_file"> 
    <a href="delete_file/delete_file.php?student='.$student_id.'&agent='.$agency_id.'&file='.$file_id.'" class="delete-file"><i class="fa fa-times-circle-o"></i></a> 
    <a class="the-file student_'.$student_id.'" id="file_'.$file_id.'" href="/application/student/file_management'.$file_path.'" target="_blank"><i class="fa fa-file-archive-o"></i></i></i>'.$file_name.' <span>'.$file_size.'</span></a> 
</li> 

delete_file_ajax.js

$(document).ready(function() { 
"use strict"; 
$(".delete-file").click(function() { 
    var container = $(this).parent(); 
    var id = $('.the-file').attr("id"); 
    var string = 'id='+ id ; 
    if(confirm("Are you sure you want to delete this?")) { 
    $.ajax({ 
     type: "POST", 
     url: "delete_file/delete_file.php", 
     data: string, 
     cache: false, 
     success: function(){ 
      container.slideUp('slow', function() {$(this).remove();}); 
    } 

     }); 
    } 
    return false; 
    }); 
}); 

delete_file.php

<?php 
session_start(); 

//Connect to Database 
require_once('../../../../db_config.php'); 
$db_connect = connectDB($mysqli) or die(mysqli_error()); 
//First lets make sure the user is allowed 
require_once('../../../../auth/admin_session.php'); 

//Create our session on session_id 
$session_id = $_SESSION['ADMIN_ID']; 

//Get the IDs 
$s_id = $_GET['student']; 
$a_id = $_GET['agent']; 
$f_id = $_GET['file']; 


//Lets find the Id of the Agency User 
$session_query = mysqli_query($db_connect, "SELECT * FROM agency WHERE agency_id = $a_id"); 
$session_row = mysqli_fetch_assoc($session_query); 
$session_num_rows = mysqli_num_rows($session_query); 

$agency_id = $session_row['agency_id']; 

//Lets find the Id of the Agency User 
$student_session_query = mysqli_query($db_connect, "SELECT * FROM students WHERE student_id = $s_id"); 
$student_session_row = mysqli_fetch_assoc($student_session_query); 
$student_session_num_rows = mysqli_num_rows($student_session_query); 

$student_id = $student_session_row['student_id']; 

//Lets find the Id of the File we want to delete 
$file_session_query = mysqli_query($db_connect, "SELECT * FROM uploaded_files WHERE file_id = $f_id AND agency_id = $a_id AND student_id = $s_id"); 
$file_session_row = mysqli_fetch_assoc($file_session_query); 
$file_session_num_rows = mysqli_num_rows($file_session_query); 

$file_id = $file_session_row['file_id']; 

if(!mysqli_connect_errno()){ 
    $stmt = $db_connect->prepare("DELETE FROM uploaded_files WHERE file_id = ? AND agency_id = ? AND student_id = ?") or die('We Could not locate the file you wish to delete'); 
    $stmt->bind_param('iii', $file_id, $agency_id, $student_id); 
    $stmt->execute(); 
    $stmt->close(); 
} 
?> 

解决方案

HTML

echo '<form class="delete-student-file" action="delete_file/delete_file.php" method="post">'; 
    echo '<li class="col student_file">'; 
     echo '<input type="hidden" name="student-id" value="'.$student_id.'">'; 
     echo '<input type="hidden" name="agency-id" value="'.$agency_id.'">'; 
     echo '<input type="hidden" name="file-id" value="'.$file_id.'">'; 
     echo'<a class="the-file student_'.$student_id.'" id="file_'.$file_id.'" href="/application/student/file_management'.$file_path.'" target="_blank"><i class="fa fa-file-pdf-o"></i>'.$file_name.' <span>'.$file_size.'</span></a>';       
     echo '<button class="delete-file" name="submit"><i class="fa fa-times-circle-o"></i></button>'; 
     echo'</li>'; 
echo'</form>'; 

delete_file.php

//Get the IDs 
$s_id = $_POST['student-id']; 
$a_id = $_POST['agency-id']; 
$f_id = $_POST['file-id']; 

delete_file_ajax.js

$(document).ready(function() { 
    "use strict"; 
    $(".delete-file").click(function(e) { 
     e.preventDefault(); 
     var container = $(this).parent(); 
     var formData = $('.delete-student-file').serialize(); 
     if(confirm("Are you sure you want to delete this?")) { 
     $.ajax({ 
      type: "POST", 
      url: "delete_file/delete_file.php", 
      data: formData, 
      cache: false, 
      beforeSend: function(){ 
       container.animate({'backgroundColor': '#fb6c6c'}, 300); 
      }, 
      success: function(){ 
       container.slideUp('slow', function() {$(this).remove();}); 
      } 
    }); 
     } 
    }); 
}); 
+1

你看在浏览器的控制台中的AJAX请求/响应? –

+0

AJAX请求可能包含或不包含允许PHP识别'$ _SESSION'数据的头文件。 – CollinD

+1

打开Chrome并打开devtools,然后查看网络选项卡。从那里,你可以检查请求,并1)确保你正确的文件,2)检查传递到页面的帖子/获取数据,3)检查服务器的响应。 –

回答

4

它看起来像你的问题是发送Ajax调用POST和请求其作为GET。试试这个:

$.ajax({ 
    type: "GET", 
    url: "delete_file/delete_file.php", 
    data: string, 
    cache: false, 
    success: function(){ 
     container.slideUp('slow', function() {$(this).remove();}); 
} 

我个人建议改变你的PHP接受POST而不是GET,但这只是我的看法。

+0

这没有奏效,结果相同。我试图避免发布,因为那样我就不得不使用一个表单否?我会不知何故必须通过ajax传递其他两个Id,以及我在URL中使用GET ='。$ student_id。'&agent ='。$ agency_id。'&file ='。$ file_id。'“的方式吗? – bilcker

+1

无论哪种方式,你的PHP都在请求$ _GET,而你的ajax正在发送$ _POST,你也可以在你的php页面上使用$ _REQUEST,我会建议试着看看到底发生了什么。然后触发ajax调用,从那里你可以点击实际的调用来查看你的PHP页面上发生了什么 –

+0

不是真的很确定我在找什么,但我添加了这对我的问题 – bilcker

0

PHP知道里面的“”是不是字符串变量,但附上由'可以打印可变

我建议你使用“SQL查询”。

$session_query = mysqli_query($db_connect, "SELECT * FROM agency WHERE agency_id = '$a_id'"); 

OR

$session_query = mysqli_query($db_connect, "SELECT * FROM agency WHERE agency_id = '".$a_id."'");