Reputation: 1008
Using php I am generating a table from a PostgreSQL system. The user needs to be able to have the option to delete a row in the table, so I have created a "Delete?" button option in the last column of the table.
The button looks like this:
echo '<td> <input type="submit" name="'.$row['language'].'" value="Delete?" class="odd" />';
So the button gets a name attribute with the language of that row.
I have two issues: 1. Most importantly, I am not sure how to access this field. Initially I was thinking I could get it via POST e.g name = _$POST['name'], but I need the 'language' field to get the specific button corresponding to the row. Hopefully you understood that... To clarify, the html attribute name is not generated until the query has been printed out. And then I'd need to know the name attribute to access it in the delete.php.
I've been considering hidden fields, but I'm unsure how I'd generate all the info and then how it'd be passed to my delete.php file.
If you have any other suggestions too though I'd be open to them.
Upvotes: 0
Views: 289
Reputation:
Does your table have a primary key that autoincrements and is an integer? If so, you can simply use its id
field for the name
attribute. So if your table looks like this:
id | language
---+---------
1 | English
2 | French
3 | C
4 | にほんご
5 | foobar
One line of your output can look like this:
echo '<td><input type="submit" name="'.$row['id'].'" value="Delete?" /></td>';
This depends on the target of the form, though. Is it expected to be able to delete and edit? Creating a link with GET data which then prompts for confirmation is a bit cleaner and less prone to abuse.
Upvotes: 0
Reputation: 21856
The most common solution to this is put in a link in the last column, an pasS the id of the row you want to delete in that link.
A link would look like this:
<a href="delete.php?id=rowid>delete</a>
Do not use a sumbit, as you need a different link for every row id.
Upvotes: 1