-1

I'd like to post a form with inputs and files and retrieve in the destination page in the same order i've got in the form, for example:

<form id="form" method="post" action="">
   Description file: <input type="text" name="input-1" value="description 1">
   <br>
   File: <input type="file" name="input-2">
   <br>
   <br>
   Others data: <input type="text" name="input-3" value="some others unique data">
   <br>
   Description file: <input type="text" name="input-4" value="description 2">
   <br>
   File: <input type="file" name="input-5">
   <br>
   Description file: <input type="text" name="input-6" value="description 3">
   <br>
   File: <input type="file" name="input-7">
   <br>
</form>


<?php

   $post = $_POST;
   $files = $_FILES;

   foreach ( $post as $i => $value ) {
     echo "Variables passed: " . $value . "<br>";
   }

   foreach ( $files as $i => $value ) {
     echo "File: " . $value['name'] . "<br>";
   }

?>

I'd like an output like:

Variables passed: description 1
File: file1.jpg
Variables passed: some others unique data
Variables passed: description 2
File: file2.jpg
Variables passed: description 3
File: file3.jpg

but i get this, cause i've 2 different foreach:

Variables passed: description 1
Variables passed: some others unique data
Variables passed: description 2
Variables passed: description 3
File: file1.jpg
File: file2.jpg
File: file3.jpg

This is only a simple example. I've a dynamic form where i can add inputs fields in differents ordering with a button.

Is there a way to mantain the same order to destination page?

UPDATE: i've edited my original post: It's not a rule there's always a File input after an input field text.

UPDATE 2: name like input-1, input-2, input-3, input-4, ... are automatically retrieved from mysql db when i click add button to insert a new input.. so they can be in different order like: input-1, input-3, input-2, input-4, ...

Giuseppe Lodi Rizzini
  • 1,045
  • 11
  • 33
  • Possible duplicate of [Is the order of INPUTS in a POST guaranteed for array inputs in PHP?](https://stackoverflow.com/questions/4400698/is-the-order-of-inputs-in-a-post-guaranteed-for-array-inputs-in-php) – Mitya May 08 '18 at 15:22
  • 1
    You shouldn't ever rely on what the client sends the server. Even if your page could be configured somehow to send them in a guaranteed order, I can construct my own request via the network console that will send them in an order of my choice. The server should be the boss of the client, not the other way round, and as such should be fully expectant of what's coming, and in what possible permutations. – Mitya May 08 '18 at 15:26

3 Answers3

0

Assuming that files will always be accompanied by a description, and descriptions will always be accompanied by a file (you'll need to validate against this). Something like this should point you in the right direction:

<form id="form" method="post" action="">
    Description file: <input type="text" name="input-1" value="description 1">
    <br>
    File: <input type="file" name="file-2">
    <br>
    Description file: <input type="text" name="input-3" value="description 2">
    <br>
    File: <input type="file" name="file-4">
    <br>
    Description file: <input type="text" name="input-5" value="description 3">
    <br>
    File: <input type="file" name="file-6">
    <br>
</form>


<?php

   $post = $_POST;
   $files = $_FILES;

   foreach ( $post as $i => $value ) {
     echo "Description: " . $value . "<br>";
     echo "File: " . $files[$i]['name'] . "<br>";
   }

?>
Alex Mulchinock
  • 2,119
  • 1
  • 18
  • 25
0

If you know in advance that the fields will have those names in that format, then you could do something like this:

$len = sizeof($post);
for ($i = 0; $i<len;$i++)
{
  $item = $i + 1;
  echo "Description: ".$item.": ".$post["input-".$item]."<br>";
  echo "File: ".$item.": ". $files["file-".$item]."<br>";
}

Of course if your $_POST also contains other variables, you may need to do something like use a regular expression to filter just those names which match your pattern, and only use those matches in your loop.

And as others have mentioned in the comments, you should always keep in mind the possibility of unexpected inputs.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Hi, thanks this is a solution form my example I think. But i get a mistake in my question: name always start with "input-", it's not always true are in order like input-1 input-2 input-3 input-4 ....in some cases they can be input-1 input-3 input-2 input-4 cause numbers are retrieve in a mysql db when I add a typology of input. – Giuseppe Lodi Rizzini May 08 '18 at 15:41
0

OK I've finally found a solution.

Before onsubmit, i'll use:

$("body").find(":input[name^='input-']").each(function (index, value) {

        var value = $(this).attr("name");

        $(form).append('<input type="hidden" name="varsOrder[]" value="' + value + '">');

});

form.submit();

Then in index.php:

// RETRIEVE ONLY $_POST starting with input-

foreach($_POST as $key => $value) {

    if (strpos($key, 'input-') === 0) {

        $idVars = ltrim($key, "input-");

        $vars[$idVars] = $value;

    }
}

// RETRIEVE ONLY $_FILES starting with input-

foreach($_FILES as $input => $array) {

    if (strpos($input, 'input-') === 0) {

        $idVars= ltrim($input, "input-");

        // PROCESS FILE
        ...

        $vars[$idVars] = $array['name'];

    }
}


//  FINAL REORDER

$orderedVars = $vars;

$order = isset($_POST['vars']) ? $_POST['vars'] : array();

if ( count($order ) ) {

    $orderedVars = array();

    foreach ( $order as $index => $nameVar) {

        $idVar = ltrim($nameVar, "input-");

        if ( array_key_exists($idVar, $vars) ) {

            $orderedVars[] = $vars[$idVar];

        }

    }

}

// $orderedVars AT THIS POINT IS AN ARRAY OF ORDERED INPUTS LIKE ORIGINAL FORM
Giuseppe Lodi Rizzini
  • 1,045
  • 11
  • 33