简体   繁体   中英

Passing data from one php generated form to another

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 eg 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.

  1. Even if I can get the name of the language to be deleted from the button, it is possible that there may be other languages of the same name in the database with other attributes, and so I would not want those deleted when I do a delete statement, meaning I need more information from that specific row of the table. So how would I pass more information?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM