I want to delete the file when I pressed the button. My code is below
<html>
<input type="button" value="Delete" onClick="if(confirm('Are you sure want to delete Msg<?php $file="0000"; echo $file; ?>?'))
<?php unlink("msg".$file.".wav");
unlink("msg".$file.".gsm");
unlink("msg".$file.".WAV");
unlink("msg".$file.".txt");
?>
alert('The Msg<?php echo $file; ?> is Deleted')
else alert('The Msg<?php echo $file; ?> not Deleted')">
</html>
But it not works
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:
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!
I think this question shows that there's a lack of understanding on how server side and client side code works. To achieve this sort of thing, you will need to send a request to your server, perform the action and then return some output to the user - short of re-writing your code for you, there's not a lot we can do on here.
The documentation for unlink()
is available on the PHP website: http://www.php.net/manual/en/function.unlink.php
If you search the web, there are plenty of tutorials showing you how to achieve this sort of thing. Here's an example though: http://www.tizag.com/phpT/filedelete.php