Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Create or update" form behavior when hitting back button

I have the following workflow on a website:

  1. Some user John Doe declares a company through form 1 (fields: name, head office location)
  2. After John Doe submits (HTTP POST) form 1, he is redirected (HTTP 302) to company form 2 with additional legal information about the company.

The problem is, if John Doe hits the back button of his browser during step 2, he will land on the form 1, with data filled by the browser (using values he already submitted — that's what Firefox and major browsers seem to do).

John Doe might then think he can use this form to update some information (e.g. fix a typo in the name of the company) whereas he will actually create a new company doing so, as we don't know on the server side whether he wants to declare a new company or update the one he just created.

Do you know any simple solution to handle that problem ?

like image 414
Weier Avatar asked Feb 14 '17 18:02

Weier


4 Answers

Use javascript/jquery script after the page is loaded to empty all the inputs. This will prevent confusion of "updating the company".

jQuery would look something like this:

$('#elementID').val('');
like image 192
Bram Avatar answered Nov 14 '22 16:11

Bram


You can also handle the situation by manipulating the browser history on load of form 2, and pass the CompanyId generated on submit of form 1 using querystring. So that you can actually update the company as the user

Suppose John submits form1.html, a unique CompanyId "1001" is generated and redirected to form2.html. Now on load of form2 you can modify the browser history form1.html?companyid=1001 using

var stateObj = { foo: "bar" };
history.pushState(stateObj, "page 1", "form1.html?companyid=1001");

Now, when the user click back button and submits the form1 again. you can check for companyid in querystring and update the company.

like image 29
Tej Patil Avatar answered Nov 14 '22 17:11

Tej Patil


I think it is more user-friendly when user can return back to previous form and update it (instead preventing the described behavior). I use in most cases similar way to handle described problem:

  1. Let's assume that user is on the page /some-page, that contains "Create new company" button. When the user opens this page, will be executed special method createOrFindCompanyDraft() on the server-side. This method creates new company "draft" record in DB (only for the current user). For example, draft record has primary key id=473. When you execute this method again it will return the same record with the id=473 (with "draft" status). "Draft" record should't display on any other interfaces. And "Create new company" has link /company/common/473.

  2. When user go to /company/common/473, you display form 1, that will be filled from "draft" record. At first time user will see empty form. Technically user will update the existing record, but you can display "Create new company" title on the page.

  3. Then user go to form 2, for example, /company/legal-info/473, you create similar draft record for the this form (similar to step 1).

  4. When user submit the form 2, you will remove "draft" status from the record id=473 (and any related records).

  5. Next time when user open page /some-page, will be created new draft record for the current user.

Browser history will contain:

  • /some-page
  • /company/common/473
  • /company/legal-info/473
  • /some-page2

I like this approach, because all form only update records. You can go to previous/next form many times (for example "Back"/"Forward" browser buttons). You can close browser, and open not completed forms tomorrow. This way doesn't require any additional manipulation with the browser history.

like image 1
IStranger Avatar answered Nov 14 '22 17:11

IStranger


try this

<form autocomplete="off" ...></form>

And Another

Use temporary tables or session to store the Page 1 form data. If the page 2 form is submitted use the temporary data of page 1 which is stored in database or in session.

Use a Separate key (Hidden field ) in both page 1 and page 2.

like image 1
Crazy Developer Avatar answered Nov 14 '22 16:11

Crazy Developer