Preserving Form Elements by creating hidden form elements on new page based on previous page's form submit

/ Published in: PHP
Save to your folder(s)

I made this function to render hidden form elements in a new page based on what was sent from the last form. This function can be used to preserve input from a long multiple-page form.

What does it do:

1. Go through all the submitted POST elements.
2. Draw a loop of those POST elements into this new form (in form of hidden elements).
3. When that form submits, previous page's input is included inside this new form's submit action.

Note: Do not use the same form element name as it overwrites as it goes along.

Copy this code and paste it in your HTML
  1. function retain_post_data() {
  2. foreach($_POST as $key=>$index) {
  3. if (is_array($index) == false) {
  4. $index = trim($index);
  5. $form .= "<input name='$key' type='hidden' value='".$index."' />";
  7. }
  8. else {
  9. foreach($index as $key2=>$index2) {
  10. if ($index2 != "") $form .= "<input name='".$key."[".$key2."]' type='hidden' value='$index2' />";
  11. }
  12. }
  13. }
  15. return $form;}

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.