HTML:
Owner: input type="text" name="owner[]" />
Category:
<select name="cat[]">
<option value="clothes">Clothes</option>
<option value="shoes">Shoes</option>
<option value="accessories">Accessories</option>
</select>
Upload: <input type="file" name="image[]" />
单击“+按钮”时克隆相同字段的函数
我将POST字段计为:
$num = count($_FILES['image']['name']);
因为我想知道最终用户克隆字段的次数。
我想要的是确保用户必须用“+按钮”填充他打开的所有字段我无法检查我想要检查他打开的字段的所有隐藏字段。
所以我该怎么办?
我不能这样做:
$owner = $_POST['owner'][$i];
$cat = $_POST['cat'][$i];
$file = $_FILES['image'][$i];
if ($owner && $cat && $file)
echo "bla bla bla";
else
echo "fill all the fields!";
任何人都可以帮助我吗?
谢谢
答案 0 :(得分:3)
您需要事先确定一些要点。每当您将任何输入字段的name
属性用作“owner[]
”或“cat[]
”或“image[]
”时,您将获得一个数组。但是,由于输入文件的属性访问功能默认为2D数组,因此现在您可以将这些属性作为3D数组访问。
当您为输入文件字段的[]
属性添加“name
”时,您现在将第一个文件的名称设为“$_FILES['image'][0]['name']
”,因为数组索引已启动根据您的问题,您可以使用以下方式进行验证: -
<?php
$numOwners = count($_POST['owner']);
$numCats = count($_POST['cat']);
$numFiles = count($_FILES['image']);
// Check to see if the number of Fields for each (Owners, Categories & Files) are the same
if ($numFiles === $numCats && $numFiles === $numOwners) {
$boolInconsistencyOwners = FALSE;
$boolInconsistencyCats = FALSE;
$boolInconsistencyFiles = FALSE;
for ($i = 0; $i < $numFiles; $i++) {
if (empty($_POST['owner'][$i])) {
$boolInconsistencyOwners = TRUE;
break;
}
if (empty($_POST['cat'][$i])) {
$boolInconsistencyCats = TRUE;
break;
}
if (!is_uploaded_file($_FILES['image'][$i]['tmp_name'])) {
$boolInconsistencyFiles = TRUE;
break;
}
}
if ($boolInconsistencyOwners || $boolInconsistencyCats || $boolInconsistencyFiles) {
echo "I knew that there will be some problems with users' mentality!";
// Redirect with proper Error Messages
}
else {
echo "Wow, Users have improved & have become quite obedient!";
// Proceed with normal requirements
}
}
else {
echo "Something fishy is going on!";
}
?>
希望它有所帮助。