Skip to content
Advertisement

ajax call to delete a server file returns html of current page

I want to use a click to delete eror_log file on server
In the code below why I’m getting html of currently viewing page as a response?
This happens regardles error_log exists or not
I’m expecting empty string as a response

$('.btnmoto').on('contextmenu', function(e){
    e.preventDefault();
    $.post('aa.php', {fn: 'del_erlog'}, function(data){
        console.log(data);  // html of current page
    });
});

aa.php

function del_erlog(){
    if (is_file('error_log')) {
        unlink('error_log');
    }
}

Advertisement

Answer

In the code below why I’m getting html of currently viewing page as a response?

Because that is what the PHP is outputting.


I assume that somewhere in the PHP you have some code along the lines of

if ($_POST['fn'] === 'del_erlog') {
    del_erlog();
}

But that doesn’t stop the rest of the code in the file running.

You would need an exit statement or similar to do that.

(I don’t recommend mixing Ajax handling code and HTML document generating code in the same file though, it makes things harder to manage in general).

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement