如何按类型读取表单的输入?
我有一个表单:
<form name="form1" method="post" action="form_to_write.php">
<h4>q1</h4>
<input type="radio" name="a1" value="someValue1" />someValue1<br />
<input type="radio" name="a1" value="someValue2" />someValue2<br />
<input type="radio" name="a1" value="someValue3" />someValue3
<h4>q2</h4>
<input type="radio" name="a2" value="someValue4" />someValue4<br />
<input type="radio" name="a2" value="someValue5" />someValue5<br />
<input type="radio" name="a2" value="someValue6" />someValue6
<h4>q3</h4>
<input type="radio" name="a3" value="someValue9" />someValue9<br />
<input type="radio" name="a3" value="someValue7" />someValue7<br />
<input type="radio" name="a3" value="someValue8" />someValue8
<input type="submit" value="submit" name="submit"/>
</form>
并且想按类型(无线电)读取数组的所有输入。我知道,如何按名称读取它,但如何按类型读取?
I have a form:
<form name="form1" method="post" action="form_to_write.php">
<h4>q1</h4>
<input type="radio" name="a1" value="someValue1" />someValue1<br />
<input type="radio" name="a1" value="someValue2" />someValue2<br />
<input type="radio" name="a1" value="someValue3" />someValue3
<h4>q2</h4>
<input type="radio" name="a2" value="someValue4" />someValue4<br />
<input type="radio" name="a2" value="someValue5" />someValue5<br />
<input type="radio" name="a2" value="someValue6" />someValue6
<h4>q3</h4>
<input type="radio" name="a3" value="someValue9" />someValue9<br />
<input type="radio" name="a3" value="someValue7" />someValue7<br />
<input type="radio" name="a3" value="someValue8" />someValue8
<input type="submit" value="submit" name="submit"/>
</form>
And want to read all inputs to array by type (radio). I know, how to read it by name, but how by type?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
提交表单时,输入
type
属性不会发送到服务器。仅发送名称
和值
。您需要使用有用的名称
来跟踪您自己在服务器上的内容。The input
type
attribute is not sent to the server when the form is submitted. Only thename
and thevalue
are sent. You will need to keep track of what's what yourself on the server using usefulname
s.使您的
form_to_write.php
像这样:并研究它的输出。
它包含您可以从表格中获得的所有内容。您可以自由选择使用什么。享受。
由于您的问题是一个提出得不好的问题的完美示例,因此我只能猜测您的真正需求。
看来您想要获得一个包含所有单选按钮的数组。您仍然可以通过使用名称来做到这一点。
将单选按钮名称设置为这样
,您将能够访问包含所有单选字段的
$_POST['radios']
数组make your
form_to_write.php
like this:and study it's output.
It contains everything you can get from the form. You are free to choose what to use. Enjoy.
As your question being a perfect example of a badly asked question, I can only guess your real needs.
It seems you want to get an array contains all radio buttons. You still can do it by using names.
make your radio buttons names like this
and you'll be able to access
$_POST['radios']
array which contains all your radio fields如果您正在寻找像
GetAllInputsOfType("radio")
这样的 PHP 函数,那么您将找不到它(除非您可以像 JS 一样使用 DOM 做一些花哨的事情;也许这会有所帮助?)。我在类似情况下所做的就是根据类型重命名我的输入字段,因此您可以使用 radio_a1、radio_a1、radio_a3 和 text_a4、memo_a5、listbox_a6 等代替 a1、a2、a3(顺便说一句,使用一些 < em>有意义的名称,而不是 a1、a2、a3 ;-)
然后你可以循环遍历数组 $_GET 或 $_POST 寻找开头的元素
radio_
...If you are looking for a PHP function like
GetAllInputsOfType("radio")
then you won't find it (unless you can do somethign fancy with the DOM, like JS does; maybe this will help?).What I have done in similar circumstances is to rename my input fields according to type, so instead of a1, a2, a3, you could have radio_a1, radio_a1, radio_a3 and text_a4, memo_a5, listbox_a6, etc (and, btw, use some meaningful names, not a1, a2, a3 ;-)
Then you can loop thorough the array $_GET or $_POST looking for elements beginning
radio_
...您可以使用类似 Zend_Form 之类的东西来跟踪它(甚至可以执行验证作业等)。但是您无法仅使用 php 获取表单字段的类型 - 您需要在客户端的 JS 中执行操作,并且可能不被信任。
You could use something like a Zend_Form which keeps track of it (and could even do validating jobs etc). But you can't get the type of a form field with just php - you'll need to do things in JS which is on the client side and may not be trusted.