3

在一个很长的产品列表中,我正在创建一个表格。每行都应该可以更新。如果我将多个表单放在一个表中,则 HTML 无效。一个解决方案是为每一行创建一个表,但格式会非常难看。

我想做这个:

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

这是无效的。我能做什么?

4

3 回答 3

2

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.

于 2012-06-10T17:44:19.433 回答
2

在整个表格周围使用一个表格。

过滤掉不需要的数据到达服务器后。

于 2012-06-10T15:03:15.367 回答
0

使用 id 指定行。还将更改行的 id 发送到服务器以进行过滤。

于 2012-06-10T17:10:14.550 回答