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?

Recommended Answers

All 2 Replies

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);

thanks, works well now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.