1

I am working on a multiple page form which is hosted on IIS. The whole form is on one PHP file where each form page is a separate div set to display:none. When user clicks the next button, JS adds a class to current div to hide it and show the next one. On submit, a "Thank you" page is displayed. This page is located in the includes folder within the project folder.

This is the code that displays that page:

$stmt = sqlsrv_query( $conn, $tsql_callSP, $params);
if( $stmt === false )
{
    echo "Error in executing statement.\n";
    die( print_r( sqlsrv_errors(), true));
} else {
        sqlsrv_free_stmt( $stmt);  
        sqlsrv_close( $conn); 
        header("location: success.php");
        exit();
}

It works OK but users can click the back button in the browser and resubmit if they want to. How can I prevent this from happening? I've seen on CognitoForms that the back button is disabled (or there isn't anything to go back to) after form submission.

Any tips would be much appreciated.

Thanks, Tom.

Mr T
  • 45
  • 1
  • 7
  • Does this answer your question? [How can I stop the browser back button using JavaScript?](https://stackoverflow.com/questions/12381563/how-can-i-stop-the-browser-back-button-using-javascript) – FZs Dec 02 '20 at 17:51
  • What are the rules of the form? Can anyone fill it out or only registered users? Should they only fill it out once or maybe they can fill it out multiple times, but with a time delay between submissions? – El_Vanja Dec 02 '20 at 18:07
  • @El_Vanja anyone with a link can fill out the form - no time limits but it would be on a daily basis. – Mr T Dec 02 '20 at 19:10
  • Then it's impossible to prevent it. There is the solution of using cookies, but cookies can be deleted. – El_Vanja Dec 02 '20 at 19:34
  • @El_Vanja Understood. Thanks for your input. – Mr T Dec 03 '20 at 09:09

1 Answers1

0

As @El_Vanja pointed out. It all depends on the rules of your form.

Possible Solutions

I do not think PHP can redirect without a back arrow. However, you might be able to replace the contents of your page in PHP (not a PHP expert).

Since this question is marked with "javascript" I do know that you can execute this: window.location.replace(url);. This might help if you want to execute javascript with php.

Suggestions

I think there are better solutions (after all, you just need to enter the URL again and you are back at the form).

If you are very adamant they do not revisit the page. You should save whether or not they completed the form in the database for their account. If you do not have accounts you could also track IPs, but keep in mind, this can be spoofed (then again, users can always create multiple accounts).

If you don't want them to revisit the page but aren't that worried about how many times they take the test; you can create a cookie. Just check if the cookie is set and redirect accordingly.

I would write code here... but I do not know the exact details of what you want to achieve.

However, I hope I was able to point you in the right direction.

Octal
  • 187
  • 10
  • thanks. Like you said, they can always revisit the page and resubmit, so this is more to prevent accidental resubmissions. I will check that link you shared. Thanks again. – Mr T Dec 03 '20 at 21:35