0

I've designed one HTML form as follows :

<form action="sample_test.php" method="post">
  <input type="text" name="fileName" value="8.png" id="fileName[]">
  <input type="text" name="fileLink" value="https://www.filepicker.io/api/file/zZ993JyCT9KafUtXAzYd" id="fileLink[]">

  <input type="text" name="fileName" value="2_OnClick_OK.jpg" id="fileName[]">
  <input type="text" name="fileLink" value="https://www.filepicker.io/api/file/1w3cKCW1TMmytb7md3XQ" id="fileLink[]">

  <input type="submit" name="Submit" value="Submit File">
</form>

Then the code in sample_test.php is as follows :

<?php
  print_r($_POST); die;
?>

The output I got is as follows : Array ( [fileName] => 2_OnClick_OK.jpg [fileLink] => https://www.filepicker.io/api/file/1w3cKCW1TMmytb7md3XQ [Submit] => Submit File )

But this is not the desired output. I want the desired output array to be printed in following manner:

Array
        (
            [8.png] => Array
                (
                    [0] => https://www.filepicker.io/api/file/zZ993JyCT9KafUtXAzYd
                )
            [2_OnClick_OK.jpg]
                (
                    [0] => https://www.filepicker.io/api/file/1w3cKCW1TMmytb7md3XQ
                )   

        )

For now I've just demonstrated with two elements only but in real situations hundreds of such elements could present on the form.

So what changes do I need to make in my HTML as well as PHP code? Please help me.

Thanks in advance.

2 Answers2

0

What you ask is impossible by just modifying the HTML code, because you would like a value (of fileName) to become an index in the array you get. That's impossible, the index will always be the name of the input.

However, if you have a look here : POSTing Form Fields with same Name Attribute , you will be able to get arrays of fileName and fileLink, and I'm pretty sure you can do something from there.

Community
  • 1
  • 1
Loufylouf
  • 697
  • 5
  • 13
0

A few things wrong, but you are close. Make the name field an array instead of the id - plus your ids need to be unique.

<input type="text" name="fileName[]" value="8.png" id="fileName1">
<input type="text" name="fileLink[]" value="https://www.filepicker.io/api/file/zZ993JyCT9KafUtXAzYd" id="fileLink1">

<input type="text" name="fileName[]" value="2_OnClick_OK.jpg" id="fileName2">
<input type="text" name="fileLink[]" value="https://www.filepicker.io/api/file/1w3cKCW1TMmytb7md3XQ" id="fileLink2">

Not tested, but should do the trick.

MP_Webby
  • 916
  • 1
  • 11
  • 35