dmikester1 Posted September 15, 2007 Share Posted September 15, 2007 I have a web app that is using php and javascript. I want to have a link that I can click and have it delete a file off the server. I know in php it goes like this: $myFile = "testFile.txt";unlink($myFile); But, how do I get that to run when I click on a link? Thanks Mike Quote Link to comment Share on other sites More sharing options...
sneamia Posted September 15, 2007 Share Posted September 15, 2007 HTML File: <html> <head> <script type="text/javascript"> function deleteFileAjax(filename) { scriptitem = document.createElement('script'); scriptitem.type = 'text/javascript'; scriptitem.src = 'ajax.php?filename=' + filename; scriptitem.id = 'ajax'; document.body.appendChild(scriptitem); setTimeout('document.body.removeChild(document.getElementById("ajax"))', 500); } </script> </head> <body> <a href="javascript:deleteFileAjax('log.txt');">Delete</a> </body> </html> Ajax.php <?php if (isset($_GET['filename'])) { if (unlink(htmlentities($_GET['filename']))) { echo "alert('Great success!');"; } } ?> Quote Link to comment Share on other sites More sharing options...
dmikester1 Posted September 15, 2007 Author Share Posted September 15, 2007 Awesome! Thank you, sneamia. It worked. Now one more question for you. Instead of popping up an alert to say "file deleted." Can I have it just pop text on the page that the file was deleted using ajax like how gmail or facebook works. Thanks Mike Quote Link to comment Share on other sites More sharing options...
sneamia Posted September 17, 2007 Share Posted September 17, 2007 Awesome! Thank you, sneamia. It worked. Now one more question for you. Instead of popping up an alert to say "file deleted." Can I have it just pop text on the page that the file was deleted using ajax like how gmail or facebook works. Thanks Mike Yes you could, though it would be a bitch to code without the source to your site. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.