Welcome Guest, Not a member yet? Register   Sign In
Reload Problem, user tend to submit data twice
#1

[eluser]iive[/eluser]
I have gone through the user guide about <a href="http://ellislab.com/codeigniter/user-guide/libraries/validation.html">validation</a> and I found that if we submit the form back to the same function in our controller and if user press F5 or Reload Button in their browser, it tends to insert the record twice into db...

Any solution to avoid this issue? as user might just click yes to resubmit the POST data again on the browser warning dialog.
#2

[eluser]xwero[/eluser]
Before you validate you could check if the same input is already inserted
Code:
//model
function check_duplicate($post)
{
    $query = $this->db->query('select count(*) as check from table where field1=? and field2=?',array($post['field1'],$post['field2']));
    $row = $query->row();
    return ($row->check == 0)?false:true;
}
You could abstract it more so you can use it for all your tables.
You could even make a ajax validation for this.
#3

[eluser]Référencement Google[/eluser]
There is a way to do giving a unique ID to the submited form (in a hidden field for exemple, and generated by a microtime function) Then you have to check that the unique ID wasn't already submited.
#4

[eluser]phester[/eluser]
Possibly redirect the user to a different page after the form is validated and inserted?
#5

[eluser]Référencement Google[/eluser]
redirecting is one step, but this do not prevent the user to hit the back buttun of the browser and submit the form again, that's why the real unique solution is with a unique ID on the submited form.
#6

[eluser]xwero[/eluser]
The unique id has to be stored so you have add a field to the database with no meaning once the form is submitted and they have moved on. Or am i seeing this wrong?
#7

[eluser]Référencement Google[/eluser]
I never done it, only read some discussions about this in the past, but I would personally try with sessions variable to do this. If I can find again the forum thread wich was explaining how to do it, I will post here.
#8

[eluser]iive[/eluser]
what I am doing now is using redirect technique.. I have a processing function in my controller and after we have validated and done all the db queries then this function will redirect the user to formDisplay function which display the form(with errors if any).

Basically, my form's action will pass to the processing function instead of the formDisplay to do all the validation and processing.
#9

[eluser]obiron2[/eluser]
I have the same problem, and I am interested to understand how reridcting to the processing rather than the display avoids the issue. You will still be submitting data.

As already stated, using a hidden field requires that the hidden value be stored and validated which can be made generic but still has to be called explicity each time you want to save data.

Is there any other way to avoid this?
#10

[eluser]iive[/eluser]
hi obiron2,

here is what I do but I wonder my approach is good or not..

let's say I am doing a login form.. I have 1 controller called User.. and there are 2 functions inside...
a) function login (will call a view which display the login form)
b) function processLogin (will do the validation and process the login)

Once the user submit the form, the form will proceed to user/processLogin and we have validation rules there. If the use failed on our validation rules, processLogin function will capture the errors inside CI's validation object and store them into session then it redirects user back to login function. login function will capture the errors in the session and display the form...

That is what I am doing now to avoid the reload problem... I hope you can get what I mean




Theme © iAndrew 2016 - Forum software by © MyBB