Reputation: 494
I am currently working on a Directory Browser using PHP and I've stumbled upon a roadblock. I would like to be able to have a user select files using checkboxes, then delete them when they click a button, however I'm not sure how to go about this due to being new to PHP and web development in general. Here is my code so far for the user interface:
<a href="#" onClick="rec('deletestuff')">
<span title="Delete Selected" class="fa fa-trash fa-lg"></span>
<!--Delete Selected-->
</a>
</li>
</ul>
<script type="text/javascript">
function rec(deletestuff) {
$('#newCode').load('delete_selected.php' deletestuff);
}
</script>
<span class="selected col-md-2 col-sm-2 col-xs-1 text-right">
<form method="get">
<input type="checkbox" name="selected[]" value="'.$file.'">
</form>
</span>
then in a seperate PHP file I wrote this algorithm to handle the delete functionality
<?php
foreach($_POST['selected[]'] as $file) {
if(file_exists($lister->getDirectoryPath() . $file)) {
unlink($lister->getDirectoryPath() . $file);
echo "<script type='text/javascript'>alert('Files deleted
successfully.');</script>";
}
elseif(is_dir($file)) {
rmdir($file);
echo "<script type='text/javascript'>alert('Files deleted successfully.');</script>";
}
}
?>
I am pretty sure that I am not performing the handling between the several different types of scripts correctly (specifically having the javascript send its information to the php), however I need some pointers as to where exactly I am failing at this in order to correct it. If needed, I can share more of my source code as it is based on the PHP DirectoryLister found here: https://github.com/DirectoryLister/DirectoryLister
Thanks!
Upvotes: 0
Views: 120
Reputation: 145
First, make sure that you're sending the right data to your PHP script. Currently, on click you're calling the function 'rec' and passing it the string 'deletestuff'. You're then passing that string to your script and trying to reference the list of selected checkboxes in your script which you never passed.
As for passing the list of selected checkboxes to your script, there are a number of ways to do that. Since you're already using jQuery, I would recommend iterating through all the checkbox elements
$('input[type=checkbox]').each(function () {
if (this.checked) {
toDelete.push(this.id);
}
});
seeing if they have the attribute checked equal to true, building a list of 'toDelete' from those, and then passing that list to the script through your function.
Upvotes: 1