2

Hi I am trying to run a delete script that will delete the record from my database using ajax and php. Used without the ajax javascript file the delete_file.php script works fine and is removed from the database.(So I am asssuming the problem lies in the javascript file somewhere.) Once I add the javascript file only it appears to run and delete_file.php does notk. Basically I pass three id's through to delete_file.php find them all in their respective tables then use those variables to find the right file to delete. Any Help is greatly appreciated. I need a fresh pair of eyes, thanks

What I am Clicking [![enter image description here][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();
}
?>

Solution

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();});
            }   
    });
        }
    });
});
4
  • 1
    Did you watch the AJAX request / response in the browser's console? Commented Feb 19, 2016 at 21:25
  • The AJAX request may or may not include the headers that allow PHP to identify $_SESSION data. Commented Feb 19, 2016 at 21:27
  • 1
    Open up Chrome, and open devtools, and look in the network tab. From there, you can inspect the request and 1) make sure you're hitting the right file, 2) check the post/get data passed to the page, and 3) check the response from the server. Commented Feb 19, 2016 at 21:30
  • Maybe this is just a taste thing, but I would try to pass all of the data you need from the front-end (student, agent, file) in the data parameter of your ajax call and then retrieve them in your php that way. It's probably also easier to debug that way since you can test if the data is actually being passed properly in your browser console. Commented Feb 20, 2016 at 17:06

2 Answers 2

4

It looks like your problem is sending the ajax call as POST and requesting it as GET. Try this:

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

Personally, I would suggest changing your PHP to accept POST rather than GET, but that is just my opinion.

Sign up to request clarification or add additional context in comments.

6 Comments

This did not work, same result. I was trying to avoid posting as then I would have to use a form no? Would I somehow have to pass the other two Id's through ajax as well as I use GET on student='.$student_id.'&agent='.$agency_id.'&file='.$file_id.'" in the url?
You can use POST with ajax. Either way, your PHP was requesting $_GET, and your ajax was sending $_POST. You could also just use $_REQUEST on your php page as well. I would suggest trying to see exactly what is happening. Open up chrome dev tools, go to the network tab. Then, trigger the ajax call. From there you can click on the actual call to see what is happening on your PHP page.
not Really sure what I am looking for, but I added it to my question
@bilcker Sorry, I should have gone into more detail. Do the steps above, then when you view the network traffic, click on the response tab. It should go headers, preview, response. On the PHP page, output errors, and print_r($_REQUEST). That will tell you if your ajax call is even sending the data.
I ended up changing my method to post and it worked. Thanks for your help. I will post my solution.
|
0

PHP know inside "" is string not variable,but enclose by '' can print variable

I advice you to use '' in sql query.

$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."'");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.