简体   繁体   中英

Passing an HTML form and JS array to PHP

I have for example fields in my HTML form:

  • GroupName = Group1
  • GroupElements = 2
  • Text1_1 = empty
  • File1_1 = blah.jpg
  • Text1_2 = Some text
  • File1_2 = empty

  • GroupName = Group2

  • GroupElements = 1
  • Text2_1 = empty
  • File2_1 = blah2.jpg

User picks a group name, then decides of a number of elements, for each element he might choose to attach a file OR type in some text.

Later on I have a javascript array like this:

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)

At this point, I have yet no idea how to pass this data to .php file. I need to pass the whole array both with mixed-type elements (text or file)

You can change the way in which the fields are named: Here are some examples:

files fields are named differently to facilitate access to information about files posted

 <?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> 

And here are the results of test

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 

Anas

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM