1

hello. i have a code that looks like this:

$localdir = 'C:/somedir';
$dh = opendir($localdir);
echo '<table><form method="post" action=""><input type="submit" value="Add checked" name="doneA" />';
while(($files = readdir($dh)) != false) {
  if ($files != "." && $files != "..") {
    echo '<tr><td><input type="checkbox" name="' . $files . '" /></td><td>' . $files . '</td></tr>';
  }
}
echo '</table>';

rewinddir($dh);
unset($files);

if(isset($_POST['doneA'])) {
    
  while(($files = readdir($dh)) != false) {
  echo $_POST[$files];
  }
  
  
}
echo '</form>';
closedir($dh);

however, i het the unidentified index error all the time like the checkboxes haven't been submitted. any ideas why is that?

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Deey
0

Not knowing exactly what you want to do with the selected list of files, I made some changes based on ideas I had of what you might be doing with the list. Hopefully it points you in the right direction.

$localdir = 'C:/somedir';
$dh = opendir($localdir);

// --------------------------------------------------------------------------
// Not sure what you're wanting to do with the files but whatever it is you probably want to do it before re-listing the files and definitely not within your form. If the form wasn't posted, this won't be done and just the listing below will be performed. If it was posted, act on it now, then list the files (maybe you are doing something which will change the list of files and you want to do that before displaying them again).

if(isset($_POST['doneA'])) {
  // Form was submitted. Perform processing on selected files.  
  foreach($_POST['filenames'] as $filename) {
    // Do what ever processing on the file that you want to do.
    echo $filename . " Checked<br />";
  }
}
  
// ---------------- End processing of files -----------------------------------

// Now list your files.

// Incase your processing above changed this.
rewinddir($dh);

echo '<table><form method="post" action=""><input type="submit" value="Add checked" name="doneA" />';
while(($files = readdir($dh)) != false) {
  if ($files != "." && $files != "..") {
    // you want a consistent name for your related checkboxes and indicate it's an array, the value is what changes.
    echo '<tr><td><input type="checkbox" name="filenames[]" value=' . $files . '" /></td><td>' . $files . '</td></tr>';
  }
}
echo '</table>';
echo '</form>';
closedir($dh);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.