1

I'm trying to check what checkboxes are checked, and perform a file deletion depending on what checkboxes are checked. To me, it seems like they ALWAYS have a value even if their not checked if I got the value".." in the checkbox. I currently removed the value in the checkbox, didn't seem to make any difference.

Every checkbox is given a unique number, at the same time file its supposed to delete have the same index with its file name as data. But it NEVER sees that any of the checkboxes are even checked.. No errors, just not doing anything.. Please help

how it looks now picture

HTML and php:(document im working on is called protected.php)

<form action="protected.php" method="post"><br>

     <input type="submit" name="Delete" value="Delete">

</form>


<?php

echo 'My files';
"<br>";
$mydr = "D:\wamp\www\\";
$dir = $mydr . $_SESSION['userid'];

// Open a directory, and read its contents
$checkbox_gen = 0;
$filename_gen = array();

if (is_dir($dir)){
  if ($dh = opendir($dir)){
    while ((($file = readdir($dh)) !== false)){

        if($file != '.' && $file != '..'){
          echo "<form><input type=\"checkbox\" name=\"$checkbox_gen\" id=\"checkbox\">" . "<a href=\"$dir/$file\">$file</a></form>" . "<br>";
          $filename_gen[$checkbox_gen] = $file;
          $checkbox_gen += 1;
        }
     }

    closedir($dh);
  }
}

?>

<br>
<form action="protected.php" method="post"><br>

    <input type="submit" name="Delete" value="Delete">

</form>

<?php

if(isset($_POST['Delete'])) {

    for($i = 0; $i < 10; $i++){
        if(!empty($_POST[$i])) {
            echo $i . 'is checked';
            //$temp = $dir . '\\' . $filename_gen[$i];
            //unlink($temp);
            echo $temp;
        }

    }
}
?>

3 Answers 3

1

You are creating a separate form for every checkbox and for the submit button. So basically you are submitting the "submit button" form and of course there are no checkboxes checked because it does not have any checkboxes.

You should remove the <form> tags from this line:

echo "<form><input type=\"checkbox\" name=\"$checkbox_gen\" id=\"checkbox\">" . "<a href=\"$dir/$file\">$file</a></form>" . "<br>";

and instead echo them outside of the loop (also make sure the submit button is in the same form).

It should look something like this, I guess:

<form action="protected.php" method="post">

<?php

...

if (is_dir($dir)){
  if ($dh = opendir($dir)){
    while ((($file = readdir($dh)) !== false)){

        if($file != '.' && $file != '..'){
            echo "<input type=\"checkbox\" name=\"$checkbox_gen\" id=\"checkbox\">" . "<a href=\"$dir/$file\">$file</a>" . "<br>";
            ...
        }
    }

    closedir($dh);
  }
}

?>

<input type="submit" name="Delete" value="Delete">

</form>
Sign up to request clarification or add additional context in comments.

3 Comments

With tags do you mean the <> or ""? Also if I removed the echo, the php will create invisible html forms? I am a beginner to PHP and HTML (been doing C only). The loop goes throu all files in the folder and echo them out, I dont see how I could this it in another way. Thanks for ur reply
My lesson is to always include ALL the forms members INSIDE the form?
You only want to have multiple forms on one page if these forms serve completely different purposes (e.g. search form and login form). But besides that all form members should be children of the same form, yes.
0

Only checked checkboxes will be posted so try

print_r($_POST)

and you will see checked checkbox with value will be there and unchecked checkboxes would not be posted. Hope that helps.

Comments

0

Each form submits its own variables and you are trying to create multiple forms. You are only submitting a delete variable within that single form. You need to echo out the form start tag with action and method before your loop (no new form tags with each checkbox/file) and just the delete/submit button and form closing tag after the loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.