Best way to prevent repopulation form after success submit? |
Hello,
I use set_value etc in my forms. After I submit a form and data saved to database I wan't to prevent repopulation the form. Is there a better way then always create 2 forms in html view with checking for successful submitting and one time without set_value helpers? I tried to PHP Code: unset($_POST); but that don't work. My solution until now: Create class MY_Form_validation and add a function: PHP Code: public function clear_field_data() In Controller after success: PHP Code: $this->form_validation->clear_field_data();
Personally I always make a redirect back to the add function in case you need to add more values. Or back to the view of the newly created item.
If someone interested, I have modified the previous function, so I can decide witch fields I want to repopulate:
PHP Code: public function clear_field_data($exclude = array()) Making a redirect is also a solution, but in this case I have to use flashdata to also show the success message. I want always to prevent making unnecessary redirects for better server performance
I prefer redirect.
If the user is returned back to the form which he just filled in, he might be confused, I think. Another reason for redirecting to another url: if your form posts to the same url as the one used for opening the form, there might be unintended behaviour when the users refreshes the browser. E.g. if you show another view after a successful POST (without redirect), the browser will come up with the original form if the user hits F5. If you set your flashdata just before the redirect, it will be available in the new page request.
I've used this in MY_Form_validation for a long time:
PHP Code: public function unset_field_data( $element ) (03-08-2018, 11:48 PM)Wouter60 Wrote: I prefer redirect. This. Your users will hate you if you don't do those redirects. Use the PRG pattern. (03-08-2018, 11:48 PM)Wouter60 Wrote: I prefer redirect. With the function in my answer I also prevent filled forms. But on this way I can offer the user more usability. For example if it might be helpful for another submissions I can easy decide to repopulate some fields. For example if a user select a group in his contactlist and want to add more than one contact. This is also a way with flashdata, but would be much more code to write. (03-09-2018, 06:36 PM)Narf Wrote:(03-08-2018, 11:48 PM)Wouter60 Wrote: I prefer redirect. I don't have complete repopulation with the function above. Only if it might be helpful for the user. See example above. Also my models check if an entry also exists in validation process. Are you still thinking in this case it isn't a fine solution? (03-10-2018, 03:32 PM)minsk832 Wrote: I don't have complete repopulation with the function above. Only if it might be helpful for the user. See example above. Also my models check if an entry also exists in validation process. Are you still thinking in this case it isn't a fine solution? Yes, there's no condition under which I would think otherwise. Even if you want to repopulate the form, it's still worth doing the redirect in order to remove the resubmit-on-refresh effect. |
Welcome Guest, Not a member yet? Register Sign In |