快速问答。
我有一个表单,其中包含动态复选框和成对的文本框。当我提交表单并print_r出$_POST信息时,这就是我得到的结果。
Array
(
[chkname_156] => chkname_156
[txtID_156] =>
[chkname_157] => chkname_157
[txtID_157] =>
[doChooseAppSvc] => doChooseAppSvc
)
正如您在上面看到的,复选框已被选中,但txtID为空。我想要完成的是这样的。如果复选框存在,这意味着它们已经被选中,但文本框是空的,我需要提醒用户。因为每个都是动态的,所以下划线后的数字会发生变化,但名称的开头保持不变。
我认为,通过执行以下操作,我已经接近了解决方案的一半。我正在使用preg_grep创建一个已选中的复选框数组。以下是代码。
$pm = preg_grep("/^chkname.*/", $p);
现在,我卡住了。任何帮助都将不胜感激。
发布于 2013-10-17 00:01:27
我建议在表单中使用数组。
这可能会使比较值变得更容易。
例如:
<input type="checkbox" name="chkname[<?=$id?>]" value="" />
<input type="text" name="txtID[<?=$id?>]" value="" />
然后,对于每个迭代,您可以检查:
if (isset($posted['chkname'][$id]) && $posted['txtID'][$id]=='') {
// alert the user
}
编辑:
下面是一个检查输入的循环示例:
// loop through all text inputs
foreach ($posted['txtID'] as $id => $txtID_value) {
// for each text input, validate against checkbox with the same $id
if (isset($posted['chkname'][$id]) && $txtID_value!='') {
// output an error message
echo "<p>You checked box #$id but didn't fill in text input #$id.</p>";
}
}
https://stackoverflow.com/questions/19415908
复制相似问题