0

I want to enable users to remove a file from the server using the click of a button.

<button onclick="......">Remove file</button>"

The removal of a file is possible through PHP's unlink($filename) function. Can I somehow trigger the PHP code from the button onclick? Sorry if question is stupid, I'm new to this...

user1378110
  • 11
  • 1
  • 2
  • 3
    Sure thing, use ajax to trigger a php file. – 1321941 Jun 26 '12 at 22:50
  • You can, you need to use [Ajax](http://en.wikipedia.org/wiki/Ajax_%28programming%29). See jQuery's [Ajax method](http://api.jquery.com/jQuery.ajax/). jQuery is a javascript library, that among many other things helps facilitate ajax requests that are cross browser compatible. – drew010 Jun 26 '12 at 22:51
  • 1
    onclick is clientside javascript, and php is serverside, so there no direct connection, but whit ajax you can let the client do a new request to the server, and have that request running a php script that dose the unlink – Puggan Se Jun 26 '12 at 22:54
  • Remember, that for security reasons, you should always use POST-type AJAX calls for deleting files. Such operations should never be called as GET! – trejder Jun 26 '12 at 22:58

2 Answers2

0

if you don't want to redirect the page you can use jquery $.ajax() function, i think an example will help the OP,

    <input type="button" id="deletefile" value="Delete">​​​​​​​​​​​​
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"/>
    <script type="text/javascript">
    $("#deletefile").click(function() {

        $.ajax({   
                        type:"POST",
                        url:"ajax/filename.php",
                        data:{fileid:deletefileid},
                        cache:false,
                });


    });​

    </script>

and in filename.php you can write code to delete the file.

Sibu
  • 4,609
  • 2
  • 26
  • 38
-1

i dont think you dont need ajax for this. where your button is named 'delete':

if(isset($_POST['delete'])) { 
header ("Location: ./YourDeleteScript");
}