Sorting form elements

If I pass form elements to a script and I want to sort specific ones based on the order of the hidden sort tag:

<input type="HIDDEN" name="sort" value="order:realName,address,address_,city,state,zip,...

How would I do this? The names in the sort are the names of selected hidden inputs I want to sort that were passed!
sargentoAsked:
Who is Participating?
 
Hamlet081299Connect With a Mentor Commented:
Here's a simple example that MAY be what you are after.

You will notice that the hidden fields are in a nonsense order, but when you click the Submit button they are listed in the order specified.

<form action='test.php'>

<input type="hidden" name="zip" value="2000">
<input type="hidden" name="realName" value="John Smith">
<input type="hidden" name="address_" value="Belrose">
<input type="hidden" name="city" value="Sydney">
<input type="hidden" name="state" value="NSW">
<input type="hidden" name="address" value="12 Main Street">

<input type="hidden" name="sort" value="realName,address,address_,city,state,zip">

<input type="submit">
</form>

<?php

$order = explode(',', $sort);
foreach ($order as $varname) {
    $value = $$varname;
    echo "$varname = $value<br>\n";
}

?>
0
 
Hamlet081299Commented:
Can you give some more details?

Do you mean that you want to output the values entered in the form according to the specified order?  Or are you trying to create a form with the inputs in that order?
0
 
sargentoAuthor Commented:
I want to output the values according to the sort order.
0
 
Hamlet081299Commented:
p.s. I dropped the "order:" from the start of the sort value.  I don't know if this is needed.  If it is then just strip it off before doing the explode().
0
 
sargentoAuthor Commented:
That's exactly what Iwant! Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.