在HTML / PHP表单中管理多个类似输入的标准方式是什么?

编程入门 行业动态 更新时间:2024-10-27 12:34:05
本文介绍了在HTML / PHP表单中管理多个类似输入的标准方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个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表单中管理多个类似输入的标准方式是什么?

本文发布于:2023-10-12 02:39:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483528.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   表单   类似   方式   标准

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!