Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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');}
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading