验证多个动态创建的字段

时间:2014-06-19 04:27:27

标签: php mysql validation

我有一个包含动态创建字段的表单,其中一个字段是文件输入字段。

对于非动态创建的输入字段,我使用它来在服务器端进行验证:

if(isset ($_POST['submitform']))
    {
        $error = array();

        foreach($_POST as $key => $value) 
            {
                $data[$key] = filter($value);
            }

        if(empty($data['randominputfield']))
            {
                $error[] = "<p>Input something here</p>";
            }

        $target = "path/to/folder/"; 

        $fileinput = ($_FILES['fileupload']['name']);
        $filebasename = substr($fileinput, 0, strripos($fileinput, '.')); 
        $fileextension  = substr($fileinput, strripos($fileinput, '.'));
        $filesize = $_FILES["fileupload"]["size"];
        $fileformats = array('.png','.jpeg','.jpg','.gif');

        if (in_array($fileextension, $fileformats) && ($filesize <= 350000))
            {
                // Rename file
                $fileinputnewname = md5($filebasename) . $fileextension;
                if (file_exists("path/to/folder/" . $fileinputnewname))
                    {
                        $error[] = "<p>A file with this name already exists</p>";
                    }
                else
                    {
                        $filemove = move_uploaded_file($_FILES["fileupload"]["tmp_name"], "path/to/folder" . $fileinputnewname);
                    }
            }

        elseif (empty($filebasename))
            {
                $error[] = "<p>Please select a file to upload</p>";
            }

        elseif ($filesize > 350000)
            {
                $error[] = "<p>Files should not exceed 350kB</p>";
            }
        else
            {
                $error[] = "<p>Only files with the extensions ". implode(', ',$fileformats)." are allowed</p>"; 
                unlink($_FILES["fileupload"]["tmp_name"]);
            } 


        if(empty($error))
            { 
                //perform database insertion
            }
    }

并且效果很好。

但是现在我想让用户有机会添加无数个字段(我用jQuery完成了这个),这样他们就可以同时上传多个数据集(即随机输入和文件)时间,我想验证,然后继续存储数据库中的每组数据。有任何想法吗?

1 个答案:

答案 0 :(得分:0)

我喜欢将此方法用于动态数量的输入......

首先让你的jQuery以这种方式在表单上附加每个数据集:

//On each click at the "plus" button append two inputs like these ones
<input name="dynamicFields[fileDescription][]" placeholder="File description..."><br>
<input type="file" name="dynamicFields[file][]">

在服务器端,您将进入$_POST var两个数组,其中一个包含文件描述,另一个包含文件。您可以使用此方法对数据集进行分组:

public static function parseInputTable($inputTable)
{
    $result = array();

    foreach ($inputTable as $columnName => $columnValues) {
        foreach ($columnValues as $rowIndex => $columnValue) {
            $result[$rowIndex][$columnName] = $columnValue;
        }
    }

    return $result;
}

例如:

var_dump(parseInputTable($_POST['dynamicFields']));

输出

array(3) {
  [0] => array(2) {
    ['fileDescription'] => string(4) "foo1",
    ['file'] => /*file 1*/
  },
  [1] => array(2) {
    ['fileDescription'] => string(4) "foo2",
    ['file'] => /*file 2*/
  },
  [2] => array(2) {
    ['fileDescription'] => string(4) "foo3",
    ['file'] => /*file 3*/
  }
}