在HTML / PHP表单中管理多个类似输入的标准方式是什么? [英] What is the standard way to manage multiple similar inputs in a HTML/PHP form?
问题描述
我有一个HTML表单,允许用户通过一次提交插入或更新数据库中的多行。然而,我不知道什么是正确的或行业标准的方式来匹配输入名称与特定的行。
我现在实施的方式是给每个都有一个人类可读的名字,后面跟着一个分隔符(在我的例子中是一个下划线_字符),后面跟着行号或其他唯一标识符,以将几个输入绑在一起。然后,在处理脚本中,使用 $ field [0]对每个名称执行
获取名称并 $ field = explode(_,$ name)
$ field [1]
以获取唯一ID。
但是,在我过去的编程经验中,我试图保持像这样离散的多组数据,而不是将它们绑定在一个字符串中。我现在也知道PHP接受格式化为数组的表单名称并自动将它们转换为数组。
处理这种情况的行业标准方法是什么?
< form method =b $ b $ div class =h2_lin>解决方案
POST >
< input type =textname =myinput []value =some value/>< br />
< input type =textname =myinput []value =second value/>< br />
< input type =textname =myinput []value =another value/>< br />
< input type =submit/>
< / form>
PHP
< ;?
foreach($ _POST [myinput]作为$ value){
echomyinput:。 $值。 <峰; br /> 中;
}
?>
I have a single HTML form that allows users to insert or update multiple rows in a database with a single submission. However, I don't know what the "correct" or industry standard way to match input names against certain rows is.
The way I have implemented it currently is to give each a human-readable name, followed by a delimiter (in my case an underscore _ character), followed by the row number or some other unique identifier to tie the several inputs together against one row. Then, in the processing script, I do $field = explode("_",$name)
against each name, using $field[0]
to get the name and $field[1]
to get the unique ID.
However, in my past programming experience I have attempted to keep multiple sets of data like this discrete, instead of tying them together in a single string. I also now know that PHP accepts form names formatted as arrays and converts them into arrays automatically.
What is the industry standard method of handling this situation?
HTML
<form method="POST">
<input type="text" name="myinput[]" value="some value"/><br/>
<input type="text" name="myinput[]" value="second value"/><br/>
<input type="text" name="myinput[]" value="another value"/><br/>
<input type="submit"/>
</form>
PHP
<?
foreach ($_POST["myinput"] as $value) {
echo "myinput : " . $value . "<br/>";
}
?>
这篇关于在HTML / PHP表单中管理多个类似输入的标准方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!