PHP遍历HTML文本框的数组 [英] PHP loop through array of HTML textboxes

查看:498
本文介绍了PHP遍历HTML文本框的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复选框领域,所有这些都封装在提交表单中的一些动态的HTML表单。当提交表单时,我想通过每个复选框字段的值与循环PHP脚本。与此同时,我必须保持与复选框字段相关的某个ID,这样,当我通过每一个在我的脚本循环,我可以使用该ID在我的数据库更新正确的行。目前,我有:

I have an HTML form with a dynamic number of checkbox fields, all of which are encapsulated within a submit form. When the form is submitted, I want to loop through the values of each checkbox field with a PHP script. At the same time, I have to keep a certain ID associated with the checkbox field so that when I loop through each one in my script, I can use the ID to update the correct row in my database. Currently, I have:

<input checked="checked" name="attended_<?php echo($pid); ?>"

我只是不知道如何继续和访问所有参加[]值从我的PHP脚本(并保持ID在同一时间)。我会用一个多维数组像下面这样?

I'm just not sure how to go ahead and access all of the attended[] values from my PHP script (and keep the ID at the same time). Would I use a multidimensional array like the following?

<input checked="checked" name="attended[<?php echo($i); ?>][<?php echo($pid); ?>]; ?>"

我倒是AP preciate有这方面的帮助。谢谢!

I'd appreciate any help on this. Thanks!

推荐答案

通常当我创建复选框,我把一个索引的名字。这样,您就可以遍历您submittion code分别复选框。

Usually when I create checkboxes I put an index in the name. This way you can loop through each checkbox in your submittion code.

<input type="checkbox" name="cbGroup[1]" value="y" />
<input type="checkbox" name="cbGroup[2]" value="y" />
<input type="checkbox" name="cbGroup[3]" value="y" />

和你的PHP

foreach($_POST['cbGroup'] as $index=>$checkbox) {}

您会想要确保你的$ _ POST ['cbGroup']设置虽然,因为如果没有选中该复选框也不会是。

You'll want to make sure your $_POST['cbGroup'] is set though because it won't be if the checkbox isn't checked.

编辑:对不起,我应该学会阅读问题完全嘿嘿:)我用多维数组在PHP与HTML的输入所有的时间,我认为这是我要走的路。

Sorry, I should learn to read the question fully heh :) I use multidimensional arrays in PHP with HTML inputs all the time and I think it's the way I would go.

这篇关于PHP遍历HTML文本框的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆