• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best way to prevent repopulation form after success submit?

#1
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()
{
 
   $_POST = array();
 
   $this->_field_data = array();
 
   return $this;


In Controller after success:

PHP Code:
$this->form_validation->clear_field_data(); 
Reply

#2
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.
Reply

#3
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())
{
    if(count($exclude) == 0)
    {
        $_POST = array();
        $this->_field_data = array();
    }
    else
    {
        $repopulate_data = array();
        foreach($exclude as $element)
        {
            if(isset($this->_field_data[$element]))
            {
                $repopulate_data[$element] = $this->_field_data[$element];
            }
            elseif(isset($_POST[$element]))
            {
                $repopulate_data[$element] = $_POST[$element];
            }
        }
        $_POST = array();
        $this->_field_data = array();
        foreach($repopulate_data as $element => $value)
        {
            $_POST[$element] = $value;
            $this->_field_data[$element] = $value;
        }
    }
    return $this;


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 Smile
Reply

#4
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.
Reply

#5
I've used this in MY_Form_validation for a long time:

PHP Code:
public function unset_field_data$element )
{
    if( 
is_array$element ) ){
        foreach( 
$element as $x )
            
$this->unset_field_data$x );
    }else{
        if( 
$element == '*' ){
            unset( 
$this->_field_data );
        }else if( isset( 
$this->_field_data[$element] ) ){
            unset( 
$this->_field_data[$element] );
        }
    }

Reply

#6
(03-08-2018, 11:48 PM)Wouter60 Wrote: 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.

This.

Your users will hate you if you don't do those redirects. Use the PRG pattern.
Reply

#7
(03-08-2018, 11:48 PM)Wouter60 Wrote: 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.

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.
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.

This.

Your users will hate you if you don't do those redirects. Use the PRG pattern.

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?
Reply

#8
(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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.