Manuel
Manuel

Reputation: 31

Multiple form within one HTML table

Out of a really long product list I am creating a table. Each row should be possible to update. HTML is not valid if I put multiple forms within one table. A solution would be to create for each row a single table, but the format would be really ugly.

I would like to make this:

<table>
<tr><form><td><input type=\"text\" name=\"name\" value=\"".$row['name']."\" /></td>\n";
</td><td></td>....<td><input type=\"submit\" name=\"Submit_btn\" id=\"Submit_btn\" value=\"Update\"></td></tr></form>
<tr><form><td></td><td></td>....<td><input type=\"submit\" name=\"Submit_btn\" id=\"Submit_btn\" value=\"Update\"></td></tr></form>
.
.
.
</table>

It is not valid. What I could do?

Upvotes: 3

Views: 2649

Answers (3)

goat
goat

Reputation: 31813

If you want to do this cross browser, without using javascript:

Use a differently named submit button for each row, where the name contains id info. Like

<input type="submit" name="id[589] value="update this row">

So long as the user either clicks that submit button, or tabs to the button tpo give it focus, and then presses enter, the browser will reliably send the name of that submit button(and only that submit button), and then you can get the id via

$id = key($_POST['id']);

But, the problem is if the user submits the form via the enter key, when the form focus is on a different type of input element, like a type=text for example. The form will submit, but different browsers will either

1) send no name of any submit button at all,

2) or they will send the name of the first submit button to appear in the form's html source.

If you place a dummy submit button at the start of the form, and hide w/ css, then you can reliably detect this type of form submission via

isset($_POST['dummySubmit']);

You cannot perform the update, but you can output all the posted values back to the user(so that their work isn't lost), along with a message telling them they need to explicitly click a submit button, and let them try again.

Or... you could just forgo using individual buttons, and update every record, every time, which probably isn't as bad as you think. I don't think there's generally much benefit to try to filter out what records have changed in some attempt to reduce sql queries. But if you really wanted to, maybe send an md5 hash of the row data concatenated together, and then recomput the hash upon submit and compare.

Upvotes: 2

panda
panda

Reputation: 1344

Use the id to specify the row. Also send the id of changed row to server for filter out.

Upvotes: 0

Quentin
Quentin

Reputation: 943470

Use one form around the entire table.

Filter out the data you don't need after it gets to the server.

Upvotes: 2

Related Questions