Answer
Solution:
I found the mistake in your script. Here is the answer, hopefully this will solve it (if there is not another mistake somewhere):
Your form is sent using method="post":<form method='post' action='/category'>
In your code you check against$_GET
:
Wrong:if(isset($_GET['category_to_delete']) && $_GET['category_to_delete']!=''){
Correct:if(isset($_POST['category_to_delete']) && $_POST['category_to_delete']!=''){
Final Code must look like:
if(isset($_POST['delete'])){
if(isset($_POST['category_to_delete']) && $_POST['category_to_delete']!=''){
$category_delete = $_POST['category_to_delete'];
delete_directory('images/'.$category_delete);
echo "Deleted";
}
}
Also note that you checkisset($_POST['delete'])
-> its your button. If you dont click the button and press Enter to send the<form>
there is no$_POST['delete']
and it won't work.
One more info for you:
Your functiondelete_directory
and your form processingif(isset($_POST['delete'])){
is placed after yourforeach(glob(dirname(__FILE__) . '/images/*') as $categories_list){
This means, if you want to delete something and send the form to do so. The dropdown/select will contain the files again, because the files in directoryimages/*
will be scanned again before you really process the form (the deletion of your selected files/directory). You should put the function and the processing of the form before the category. Usually things like this will be done before any HTML/Output Processing.