|
Posted by Rik on 12/13/06 18:59
wim taerwe wrote:
> Hello,
>
> I am looking for an easy way to have a delete button per subitem in 1
> form.
>
> For example : a book can have many authors and when I edit the book
> details I want to have a list of the authors each with a separate
> delete-submitbutton next to it. Now I have the problem that I cannot
> know which exact author I want to delete when I click the delete
> button next to it.
>
> I could name every delete button differently like
> "delete_author1","delete_author2",... but what is the easiest way to
> process this after the submit.
I normally use in a list:
- a button on the top names 'delete[mass]'
- checkboxes in front named id[<id_number]
- a button named delete[<id_number>] in the row
when the logic is calles for (pseudoscript to illustrate):
if(isset($_POST['delete'])){
$ids = array();
if(isset($_POST['delete']['mass'])){
$ids = array_map('intval',array_keys($_POST['id']));
} else {
$ids[] = intval(reset(array_keys($_POST['delete'])));
}
if(!empty($ids)){
$query = 'DELETE FROM table WHERE id = '.implode(', or id =
',$ids);
mysql_query($query);
}
}
So, a 'mass' button to delete all selected, and a numbered button to delete
a specific one.
The trick is naming the inputs with the [], which will automatically create
an array for you in php's $_POST.
--
Rik Wasmus
[Back to original message]
|