3

I have a form with a submit button and a handler that stores data in the database. Problem is when the form is submitted, all data is cleared from the input fields. Is there a way to still show them after submit? What changes do I need to make to my form_submit function?

function mymodule_form_submit($form, &$form_state) {
 //how to retain the input in the form
}

I'm looking for the most "drupalish" way to get this done?

Berming
  • 1,302
  • 4
  • 18
  • 34

2 Answers2

3

As indicated by this previous StackOverflow question you can accomplish this with $form_state['storage'] and $form_state['rebuild'].

Community
  • 1
  • 1
Conspicuous Compiler
  • 6,403
  • 1
  • 40
  • 52
  • I actually read this post before posting, but had the impression it was for multi-step forms. Was I wrong to think so? – Berming Aug 27 '10 at 22:40
  • Setting the rebuild option simply retains the submitted data. I suppose I should have said "just the last part of @kidrobot's answer is applicable". Apologies for the lack of clarity. – Conspicuous Compiler Aug 28 '10 at 04:47
0

You can access the data using $_REQUEST['form_variable_name'] where form_variable_name is the name of the html input tag.

You then need to render the page back putting this value into the input tags value field.

<form method="POST" action="/account/contactdetails/">
 <div>
  <label>First name:</label>
  <input type="text" name="firstname" value="<?php echo $_REQUEST['firstname']; ?>" />
 </div>
 <div>
  <label>Last name:</label>
  <input type="text" name="lastname" value="<?php echo $_REQUEST['lastname']; ?>" />
 </div>
 <input type="submit" value="Save" />
</form>
hafichuk
  • 10,351
  • 10
  • 38
  • 53