Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

html - Is the order of INPUTS in a POST guaranteed for array inputs in PHP?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...