19

I have a form where users enter an unbounded number of rows of data. They arrive at the form by entering whatever number of rows on the screen that they desire.

<?php
$numRows = $_GET['NUM_ROWS_REQUESTED'];

?>
<form method="post">
<?php
for($i = 0; $i < $numRows ;$i++) {
  $uuid = uniqid();
?>

  <input type="text" name="MYDATA[<?php print $uuid; ?>][FIRST_NAME]" />
  <input type="text" name="MYDATA[<?php print $uuid; ?>][LAST_NAME]" />
<?php
}
?>
</form>

I'm wondering if, when the form is posted and I receive these records in the $_POST['MYDATA'] array if I can be guaranteed that they will be ordered in the same sequence as they were posted on the screen. Or will they be ordered by the uniqid() that's randomly generated?

The reason I use a unique ID instead of just integers which would be easier to sequence, is that users can remove rows and add additional rows using javascript on that page. It would be too difficult to check for collisions.

aw crud
  • 8,791
  • 19
  • 71
  • 115
  • @DampeS8N I don't really have anything to sort by. The order of inputs on the screen when they enter them is the desired order. Without doing AJAX to retrieve the system date, that won't be reliable since javascript uses the date of their PC. I was hoping I could avoid doing analysis of existing rows in javascript when creating new rows. – aw crud Dec 09 '10 at 17:03
  • 1
    You could add a hidden input with `MYDATA[][SORT_ORDER]` as name and a counter as value. You could also just use a counter as $uuid. If you still need a UUID you can let the server generate that or use the inverse of my first example: a hidden input with `MYDATA[][UUID]` as name and $uuid as value. – ontrack Dec 09 '10 at 17:11
  • 1
    link to related question with what I feel is a good accepted answer. http://stackoverflow.com/questions/3712481/what-is-the-expected-order-of-an-array-submitted-in-an-html-form – Kinjal Dixit Jun 05 '15 at 06:07

2 Answers2

11

The W3 spec doesn't include rules on what order a form's values should be assembled into a data set, so technically you cannot be sure. On the other hand, I've not seen a case (from numerous browsers on numerous OSes over the years) where data hasn't been supplied in source-listing-order. I've not really tested for cases when changing the default (UI generated) tabindex values.

You could always sort the array (asort) after you've received it to be sure about what order you're reading values.

Rudu
  • 15,682
  • 4
  • 47
  • 63
8

It's sort of guaranteed by HTML4.01 (for -urlencoded, but it's assumed to be identical for /form-data), and all current browsers submit the form fields in the order they are listed in the document.

So yes, they are ordered by their appearance, not by the random uuid.

mario
  • 144,265
  • 20
  • 237
  • 291
  • 3
    In HTML5 it's not explicit, but they often reference the "tree order" (DOM structure) for form elements: http://www.whatwg.org/specs/web-apps/current-work/multipage/association-of-controls-and-forms.html#multipart-form-data – mario Dec 09 '10 at 16:54