0

I have a view which can either display a form for creating/editing record like below:

Create

<form action="http://localhost:8000/academics" method="post" class="form-horizontal" id = "add_academic_form" role="form">

or

Edit

<form action="http://localhost:8000/academics/1" method="put" class="form-horizontal" id = "edit_academic_form" role="form">

When the user first visit the page, if there was no record, then the create form is shown.

Once the user click 'next' button, ajax is use to submit the form and redirect to the next page. Once on the next page, if the user clicks 'previous button', he is redirect back to the previous page and now since there is a record, the edit form is shown populated with the just created resource.

This works well if I trust the user to use the different buttons provided (next and previous) but becomes a problem if the user first visit the page, click next to create record, and from the next page, hit the back button of the browser and click next again which inserts the record multiple times. How can I solve this problem?

I tried an approach like so which doesn't seems to work:

function tough_guy(){
    location.reload()
    window.location.href = data.redirect
}
  • After ajax success, reload page (so that the form switch happens) and then redirect to next page. It only redirects but doesn't reload first.
Fokwa Best
  • 3,322
  • 6
  • 36
  • 51

3 Answers3

1

header("location:home.php");

instead of home put your file location that you want to go!!. it works for me

0

Unable to comment so posting as answer, I could not understand your question completely, but I hope the following helps.

Try using header

header('Location: http://yourpage.com/yourphp.php');

source: Prevent resubmit form after click "back" button

Community
  • 1
  • 1
prats1411
  • 162
  • 12
0

Use History api to remove the state or replace the state with the corresponding states. This will avoid the exact problem

https://developer.mozilla.org/en/docs/Web/API/History

udnisap
  • 899
  • 1
  • 10
  • 19