views:

43

answers:

2

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?

A: 

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
Is this "drupalish"?
Berming
+2  A: 

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

Conspicuous Compiler
I actually read this post before posting, but had the impression it was for multi-step forms. Was I wrong to think so?
Berming
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