[Solved] Delete a file when I pressed the button [closed]


There’s a few problems here.

Firstly, you don’t ever finish your onClick attribute, so this is likely to result in JavaScript errors. Check this in your console. If you are using onClick it is best to keep JavaScript brief in here — write a JavaScript function, and call it from this event.

Any PHP you have will be run when the page is served. Thus, your deletions will be done immediately, and not inside the JavaScript if statement as you want. You can achieve this in one of two ways:

  • Set up a post method form to do a trip to the server, in which you do your file deletions
  • Use an AJAX call to do a similar thing in the background. Libraries like jQuery will make this easy for you

Lastly, it is unclear how you would want the $file to be populated. If this depend on the button you press on, you’ll need to read that in either of the above approaches, and then detect the name on the server side.

As others have said, there is no substitute for sitting down for a good bit of study with a book or a video. It’s frustrating when you just want to make something, but you’ll find it a good time investment in the long term. Stick at it!

solved Delete a file when I pressed the button [closed]