将HTML表单和JS数组传递给PHP

时间:2014-10-15 09:09:41

标签: javascript php jquery arrays

我有HTML格式的示例字段:

  • GroupName = Group1
  • GroupElements = 2
  • Text1_1 = 为空
  • File1_1 = blah.jpg
  • Text1_2 =某些文字
  • File1_2 = 为空

  • GroupName = Group2

  • GroupElements = 1
  • Text2_1 = 为空
  • File2_1 = blah2.jpg

用户选择一个组名,然后决定一些元素,对于他可能选择在某些文本中附加文件 OR 的每个元素。

后来我有一个像这样的javascript数组:

array[0][0] = GroupName; //Group1
array[0][1] = GroupElements; //2
array[0][2][0] = "#File1_1";
array[0][2][1] = "#Text1_2";

array[1][0] = GroupName; //Group2
array[1][1] = GroupElements; //1
array[1][2][0] = "#File2_1"; //this is an id of first chosen element (attached file **OR** text input)

此时,我还不知道如何将此数据传递给.php文件。我需要使用混合类型元素(文本或文件)传递整个数组

1 个答案:

答案 0 :(得分:0)

您可以更改字段的命名方式: 以下是一些例子:

文件字段的名称不同,以便于访问有关已发布文件的信息

<?php 
	if (!empty($_POST)) {
		echo "ALL POSTED DATA";
		var_dump($_POST);
		
		echo "POSTED TEXT";
		var_dump($_POST['Text']);
		
		echo "POSTED TEXT / Group 1";
		var_dump($_POST['Text']['g_1']);
	}
?>
<html>
<body>
	<form action="" method="post" enctype="multipart/form-data" >
		<div>
			<h4>Group 1 / Element 1</h4>
		<input type="text" name="Text[g_1][e_1][0]" value="aaaa" /> <!-- group 1 / element 1 / text 1 -->
		<input type="file" name="File[g_1_e_1_0]"  /> <!-- group 1 / element 1 / file 1 -->
		<br/>
		<input type="text" name="Text[g_1][e_1][1]" value="bbbb" /> <!-- group 1 / element 1 / text 2 -->
		<input type="file" name="File[g_1_e_1_1]"  /> <!-- group 1 / element 1 / file 2 -->
		<br/>
		<input type="text" name="Text[g_1][e_1][2]" value="cccc" /> <!-- group 1 / element 1 / text 3 -->
		<input type="file" name="File[g_1_e_1_2]"  /> <!-- group 1 / element 1 / file 3 -->
		</div>
		
		<div>
			<h4>Group 1 / Element 2 </h4>
			<input type="text" name="Text[g_1][e_2][0]" value="bbbb" /> <!-- group 1 / element 2 / text 1 -->
		<div>
		
		<div>
			<h4>Group 2 / Element 1 & 3</h4>
			<input type="text" name="Text[g_2][e_1][0]" value="aaaa" /> <!-- group 2 / element 1 / text 1 -->
			<input type="text" name="Text[g_2][e_3][0]" value="cccc" /> <!-- group 2 / element 3 / text 1 -->
		<div>
		<input type="submit" />
		
		
	</form>
</body>
</html>

以下是测试结果

ALL POSTED DATA
array
  'Text' => 
    array
      'g_1' => 
        array
          'e_1' => 
            array
              ...
          'e_2' => 
            array
              ...
      'g_2' => 
        array
          'e_1' => 
            array
              ...
          'e_3' => 
            array
              ...
POSTED TEXT
array
  'g_1' => 
    array
      'e_1' => 
        array
          0 => string 'aaaa' (length=4)
          1 => string 'bbbb' (length=4)
          2 => string 'cccc' (length=4)
      'e_2' => 
        array
          0 => string 'G1 E2' (length=5)
  'g_2' => 
    array
      'e_1' => 
        array
          0 => string 'G2' (length=2)
      'e_3' => 
        array
          0 => string 'cccc' (length=4)
POSTED TEXT / Group 1
array
  'e_1' => 
    array
      0 => string 'aaaa' (length=4)
      1 => string 'bbbb' (length=4)
      2 => string 'cccc' (length=4)
  'e_2' => 
    array
      0 => string 'G1 E2' (length=5)

ALL POSTED FILES
array
  'File' => 
    array
      'name' => 
        array
          'g_1_e_1_0' => string 'log_10000715.txt' (length=16)
          'g_1_e_1_1' => string 'log_10001227.log' (length=16)
          'g_1_e_1_2' => string 'ts#2014-09.txt' (length=14)
      'type' => 
        array
          'g_1_e_1_0' => string 'text/plain' (length=10)
          'g_1_e_1_1' => string 'application/octet-stream' (length=24)
          'g_1_e_1_2' => string 'text/plain' (length=10)
      'tmp_name' => 
        array
          'g_1_e_1_0' => string 'C:\servers\wamp\tmp\php6DCD.tmp' (length=31)
          'g_1_e_1_1' => string 'C:\servers\wamp\tmp\php6DDE.tmp' (length=31)
          'g_1_e_1_2' => string 'C:\servers\wamp\tmp\php6DDF.tmp' (length=31)
      'error' => 
        array
          'g_1_e_1_0' => int 0
          'g_1_e_1_1' => int 0
          'g_1_e_1_2' => int 0
      'size' => 
        array
          'g_1_e_1_0' => int 229920
          'g_1_e_1_1' => int 394400
          'g_1_e_1_2' => int 1068