Hello,
I'm looking for an option to reset form values after successful submission. Actually, using javascript is rather straight forward, but was wondering if there was a way with php alone. It seems there would be...
Basically, I've just learned about this key value :
if (!empty($form_state['executed'])) {
// I could therefore use this in my AJAX callback to reset my form
}
I thought I could use drupal_get_form, but learned otherwise.
I then found a similar issue with ctools 6.x #1025448: Add an AJAX form example to the CTools ajax sample module
It seems that I would have to (re)build the form with the equivalent native 7.x drupal_build_form, but there are obviously differences between the ctools 6.x ajax framework and the new native drupal ajax framework. I've tried a few variations of the above, but it's not working for me.
Any clue would be appreciated.
Cheers
Comments
Comment #1
rfayThere are no form values after a successful submission in a normal situation, so I'm a little baffled about what you mean.
But a standard technique, used by the core search module, for example, for form submission just to redirect to a GET url and then build the page using the arguments while building the page.
What does this have to do with AJAX though?
Comment #2
rfayOh, you *can* use
$form_state['rebuild'] = TRUE;
in your submit handler, and the form will be still alive. It's a little sketchy for normal form submission, but it will work. You can also set a marker like $form_state['i_submitted_this'] = TRUE in the submit handler.
Comment #3
nlambert CreditAttribution: nlambert commentedThank you for your answer rfay.
I figured this must be the case (answer 1) and found it hard to believe it would be any different.
Now I'm pretty sure I know what the problem is... I used the webform module to quickly whip up a form. I assumed it would be no different, but since I've found hints that it might not.
I'm pretty confident that if I made a form from scratch I wouldn't have this problem. I'll do that and report back.
Comment #4
nlambert CreditAttribution: nlambert commentedWell, the fields still stay populated even with a hand made form.
Just to be sure we're on the same page, here's some pseudo code
This is my ajax callback
but I also tried the basics
I've also tried without the #ajax key. The fields are cleared after the page refresh.
I've played around with this quite a bit. My original solution was a second custom command - ajax_command_clear_form - which in jq was $('#my-form').clearForm();
My original solution works great, but I guess curiosity got the best of me. Why aren't those form values cleared in the first place?
If you do have any thoughts it would be great to hear
Comment #5
rfayI guess the question is "what does populated mean?"
If you're talking about the defaults provided on a form, there are two things.
1. Your browser very much likes to populate these.
2. $form_state['input'] is often used to populate on multistep forms, and $form_state['input'] = array() is sometimes used to clear it.
Comment #6
nlambert CreditAttribution: nlambert commentedAH! interesting
ok, so in my ajax callback I did
So the array(s) get(s) cleared, but I can still see this in the firebug POST (notice value="qsdf") :
Just for testing, I also put dsm($form_state) in my_submit callback and tried without #ajax. I noticed that $form_state['input'] still has values, but the form is cleared once submitted.
I could do this :
but then I would have to foresee every possible field.
I really appreciate your help. If you need more information for this to be any clearer, please ask.
Comment #7
nlambert CreditAttribution: nlambert commentedIf there is any lingering doubt concerning my goal, here is a sentence that should sum things up :
What I'm trying to achieve is to have the form work as if it were in an iframe (in the end the form comes up empty)
I've tried a few new things, desperate as they may seem...
or
There are tons of examples that I've looked at, but most people do it something like :
OR something similar like the ctools modal login demo.
If anyone has a thought, I'm definitely all ears.
And thanks again rfay for your time.
Comment #8
TheOptimizer CreditAttribution: TheOptimizer commentedIf I understand you well, you want to reset a field after an AJAX call.
You could try to use ajax_command_invoke in your
ajax_callback
function. Try to add something like this:$commands[] = ajax_command_invoke($selector, 'val', array(''));
$selector is then the jQuery selector of the fields you want to reset.
Comment #9
nlambert CreditAttribution: nlambert commentedHello TheOptimizer,
Thank you for your answer.
I've since resorted to a custom $commands[] function that does $(selector).clearForm().
However, I'd expect Drupal to return an empty form after $form_state['executed'] has been applied.
Imagine a form without ajax and no redirect :
I'd expect the same from an ajax form, but without the page refresh of course.
Cheers
Comment #10
adammaloneI've run into a similar issue and indeed just decided after a) a while attempting things and b) seeing this thread to go with setting
within the ajax callback. A possible way to mitigate having to input field names manually could be a foreach loop with some kind of code similar to the beneath:
This is just an idea off the top of my head and as yet I've not tried it out as I only needed to clear one field but it strikes me as a method that could work.
Comment #11
lmeurs CreditAttribution: lmeurs commentedJust setting the rebuild variable to FALSE does not return an empty form. Is it expected behavior for Drupal to automatically re-populate through AJAX submitted forms?
Helped by the earlier posts, my solution is:
Emptying the input array and setting rebuild to TRUE does return an empty form. I also tested it on default field values, these are inserted correctly as well. The submit hook is only called on submission after all validating is done, so an extra check for the value of $form_state['executed'] seems redundant.
Comment #12
eme CreditAttribution: eme commentedWell, I'm ajaxifying an entity form as well, but do not understand fully how you made it work.
I tried to have a new submit with code above (#11), and with $commands[] = ajax_command_replace(NULL, drupal_render($form)); (or with form ID instead of NULL), and my form is still full of values.
Did I misunderstood your explanations ?
Of course, it works well when emptying the form manually (ex. : $form['title']['#value'] = ''), but with entities, there may be lots of fieds, and of course not always '#value' properties...
Comment #13
Eugene Fidelin CreditAttribution: Eugene Fidelin commentedSolution #11 woks for simple forms.
If you want to clear node form after succefull AJAX request - try to use this solution http://drupal.stackexchange.com/a/36932/7313
Comment #14
eme CreditAttribution: eme commentedThanks. I succeeded to do it, and you can see the result for those who are interested you can see the result here : http://drupal.org/sandbox/eme/1594950 (module that ajaxify form entities).
Comment #15
mathankumarc CreditAttribution: mathankumarc commentedIn my case If I submit a form using jQuery then the form values are kept maintained
example, I'm using the following snippet to submit the form,
and I have one textarea, and the value of that textarea is kept maintained, however it should be cleared.
#11 solved the problem.
One more thing my form is a ajax form, if I submit the form using jQuery then the page is getting reloaded. I think the form is not submitted using ajax.
Comment #16
j0rd CreditAttribution: j0rd commentedPost #11 fixed for me. Thank you.
Comment #17
SGhosh CreditAttribution: SGhosh commentedPost #4 did the trick for me.
This is my callback function -
In JS -
Community rocks! \m/ :)
Comment #18
talgat CreditAttribution: talgat commentedI solved this problem next way. Check $form_state['executed'] && clear $form['field_name']['#value']
Comment #19
bartram CreditAttribution: bartram as a volunteer commentedI rebuild the form from the cached form, without the input values set.
Comment #20
Anonymous (not verified) CreditAttribution: Anonymous commentedbartram,
thank you! i've spent ton of time in tries to resolve this issue. and here you go. thanks!
Comment #21
sneha_surve CreditAttribution: sneha_surve commentedSolution #11 woks for simple forms.
In submit handler you need to set
$form_state['input'] = array();
It works! :D
Comment #22
PrzemyslawKot CreditAttribution: PrzemyslawKot commentedFor Drupal 8.
You can't remove all data in $form_state['input'](nor treat it like array). You have to leave id's of form and form build.
You do this like this:
Comment #23
satishb CreditAttribution: satishb as a volunteer commented$commands[] = ajax_command_invoke('#edit-comments', 'val', array(''));
return array('#type' => 'ajax', '#commands' => $commands);
worked for me ,#edit-comments is the HTML id of the field i wanted to replace