-2

There is a wizard in my project having series of forms.

When I click the submit button of the Ist form, it saves data in DB and redirects to IInd form. In IInd form, their values displayed from Ist form and have input fields and redirects to III.

I --> II  --> III  up to VII

My Question is:

Suppose User is in III form and User clicks back button of browser and then again click on next button of II form, values get inserted in DB again, because of post method.

How to handle browser back and forward button to avoid saving duplicate data. please let me know if you need some more details.

GYaN
  • 2,327
  • 4
  • 19
  • 39
SVM
  • 423
  • 1
  • 6
  • 17

2 Answers2

0

You can use firstOrCreate method while inserting the data. So your controller function would look something like this:

public function form1(Request $request){
   // validation
   $form1 = Form1::firstOrCreate($request->validated());
   return view('form2')->compact('form1);
}
// and same for second form too.

This will check to see if all the data that you are going to insert exists or not. If it exists, it will return that data, else it will create new and return that. Hope this helps.

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
Muhammad Nauman
  • 1,249
  • 8
  • 10
  • let suppose, if the 1st step of form have only first name and last name that is not unique then your logic is not working. – waseem asgar Apr 14 '18 at 16:12
0

You can store data in 3 ways -

1- Best way, Store all data in session and store in on last form submit.

2- When you store first form data, store the last inserted ID in session and check that on every step and update. On Submit of last step unset the session value.

3- Using Ajax, When you insert the first step data return the last inserted ID and using jquery add a hidden field for same and send it with every request.

Note: this is only achieved by ajax.

waseem asgar
  • 664
  • 8
  • 20