表单-单选框-复选框-提交_将表单输入数组用于复选框

表单-单选框-复选框-提交

One way that I've gotten faster at XHTML forms is using input arrays whenever possible. Input arrays allow me to deal with just one variable for checkboxes that serve as multiple answers to one question. Here's how the process works.

在XHTML表单上获得更快速度的一种方法是,尽可能使用输入数组。 输入数组使我只需要处理复选框的一个变量即可作为一个问题的多个答案。 这是该过程的工作方式。

步骤1:XHTML表单 (Step 1: The XHTML Form)

<table cellpadding="0" cellspacing="2">
	<tr>
		<td><input type="checkbox" name="how_hear[]" id="how_hear1" value="Website" <?php echo in_array('Website',$how_hear) ? 'checked="checked"' : ''; ?> /></td>
		<td style="padding-left:3px"><label for="how_hear1">Website</label></td>
	</tr>
	<tr>
		<td><input type="checkbox" name="how_hear[]" id="how_hear2" value="TV Commercial" <?php echo in_array('TV Commercial',$how_hear) ? 'checked="checked"' : ''; ?> /></td>
		<td style="padding-left:3px"><label for="how_hear2">TV Commercial</label></td>
	</tr>
	<tr>
		<td><input type="checkbox" name="how_hear[]" id="how_hear3" value="Magazine Ad" <?php echo in_array('Magazine Ad',$how_hear) ? 'checked="checked"' : ''; ?> /></td>
		<td style="padding-left:3px"><label for="how_hear3">Magazine Ad</label></td>
	</tr>
	<tr>
		<td><input type="checkbox" name="how_hear[]" id="how_hear4" value="Billboard" <?php echo in_array('Billboard',$how_hear) ? 'checked="checked"' : ''; ?> /></td>
		<td style="padding-left:3px"><label for="how_hear4">Billboard</label></td>
	</tr>
	<tr>
		<td><input type="checkbox" name="how_hear[]" id="how_hear5" value="Other" <?php echo in_array('Other',$how_hear) ? 'checked="checked"' : ''; ?> /></td>
		<td style="padding-left:3px"><label for="how_hear5">Other</label></td>
	</tr>
</table>

Note that the name attribute of each input is the same: how_hear[]. The brackets indicate that the input field is an array. Also note the PHP in each input tag that determines if the chechbox should checked initially.

请注意,每个输入的名称属性是相同的: how_hear [] 。 方括号表示输入字段是一个数组。 还要注意每个输入标记中PHP,该标记确定chechbox是否应首先进行检查。

步骤2:PHP表单验证和处理 (Step 2: The PHP Form Validation and Processing)


//initialize
$how_hear = count($_POST['how_hear']) ? $_POST['how_hear'] : array();

//echo out their choices separated by a comma
echo count($how_hear) ? implode(', ',$how_hear) : 'Nothing';


To avoid PHP warnings, we declare the $how_hear array at the very top.

为了避免PHP警告,我们在最顶部声明$ how_hear数组。

PHP's count() function returns the number of items selected, so we you require at least one be checked, count() is what we'll use.

PHP的count()函数返回所选项目的数量,因此我们需要至少检查一次, count()是我们要使用的。

Lastly, to display the selections as a list, we use the implode() function.

最后,为了将选择内容显示为列表,我们使用implode()函数。

That's it! Click here to see an example.

而已! 单击此处查看示例。

翻译自: https://davidwalsh.name/checkbox-form-input-arrays

表单-单选框-复选框-提交

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值