I’m trying to create your typical row deletion grid/table that’ll be rendered for my users.
Basically, like this, but with an extra delete button at the end of every row. (Not Pictured)
The user has the option of selecting a few checkboxes and having those elements deleted or just deleting one via the row’s delete button.
If nested forms elements were allowed, I’d just wrap the rows’s delete buttons in an <form> element. Unfortunately, you can’t do that so I’m at a loss of what to do…
How do you make the delete buttons and check boxes play nice?
Ummm are you sure links can be retained in the browser history even if you redirect with header location? And one more thing, once an item is deleted what is the problem even if the URL is again used from the browser history? I have never used this approach in any non-secured/non logged in pages yet but I haven’t thought any problems to use this way in private or logged areas.
Please don’t link through to dangerous operations. Spiders and others will happily follow through and delete your information for you.
You should only use links for immutable operations, or ones such as delete when you know with an absolute certainty that it is guarded by a button-based [yes][no] on the other end.
Agreed! But what I supposed in this case, OP has working in some private area (means after successfully login only they will see this list and delete and all). So if that is the case then I don’t think that anyone can do that outside as spam or bots or so…
By making each delete button be a submit button, where each delete button has a distintictive name and/or value, so that the server knows to take action only on that one button.
Other issues involve things such as the links being retained in the browser history. You do not want links to such places to be retained as such.
Buttons are the only viable way to deal with these things. It is possible to hide the button and use a scripted link to trigger it, but even then you have issues of trust that certain users may feel towards it.
I personally could not understand your particular requirement. You want to have a listing where each row will have a button to delete that particular row or you want a bulk deletion? I would prefer bulk deletion which makes user friendly AFAIK if there is no particular reason and requirement. To do that you just have only form in the page wrapping the whole list with <form> tag and required attributes on it. Then name the checkbox with array and put the distinct/unique id as value of the checkbox in it. Then have a submit type button at the end of the list then when you submit the form you can get the checked checkboxes and delete the rows accordingly.
For example your form will be constructed something like this:
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$itemids = $_POST['itemid'];
if(count($itemids) >= 1){
$sql = "DELETE FROM tblename WHERE id IN (" . implode(",", $itemids) . ")";
}
else{
echo 'Select at least one item';
die();
}
}
For single row deletion, I think you don’t have to have a form. Just have a link and pass the ID form URL and delete. Off course you have to take care of the passed ID before you use in the deletion statement.
If you’re going to use a link for delete then you may also use a link for edit or save or add, or other tasks too.
Redirecting with a header change might be successful on some browsers, but it pays to bot rely on the workings of web browsers for the safety of your web server.