Reputation: 37
I've built a complex multi-page web survey with multiple "conditions" (question variations) for each page. After each page submit, a separate file processes the data and inserts it into the database, after which the user is randomly directed to one condition of the next question via redir.php
. A problem with this structure is that if the user navigates back to a submitted page and resubmits, a new row is inserted into the database, and the random redirect is called again, leading the user to a (likely) different condition. For instance, question 3 has six conditions (A-F), so if for example, someone finished question 2 and was directed to 3B, then went back to question 2 and resubmitted the page, they will land on any one of 3A-3F (most likely a different condition to the referrer).
I'd like to be able to check if the user has already submitted their response, and if so, present them with an error message (along the lines of "Page already submitted. Redirecting...") and then redirect to the last page they viewed. I've looked extensively for a solution but haven't found one that can handle the multiple page conditions and randomisation. What would be the best way to do this - include a database query on each page to check if a record already exists? Use session variables?
Upvotes: 0
Views: 251
Reputation: 37
I came up with a workaround, involving storing the IDs of viewed pages in a session, then calling them from an error page if the user tries to navigate back to a completed page.
Based on the advice from doniyor and Mr. Alien (thanks) I inserted this line of code in the form processing file after the database insert:
$_SESSION['submStatus_2'] = true; //Question 2 submitted
Then at the beginning of each question page I added this code (substituting numbers in the $_SESSION
vars as appropriate):
session_start();
if(isset($_SESSION['submStatus_2']) && $_SESSION['submStatus_2'] == true) {
header('Location: redir_3.php'); //Message Page
exit;
}
$_SESSION['visited2'] = "2A"; //Page ID, current question
If the user navigates back to question 1 from question 2 (condition A), s/he will be redirected to a file redir_2.php
that contains this code:
session_start();
$last = $_SESSION['visited2'];
header("Refresh:3; url=$last.php");
and shows an error message, then redirects after a few seconds like so:
<p>Page already submitted. Redirecting...</p>
<p>If you are not automatically redirected within a few seconds, please click <a href="
<?php echo htmlspecialchars($last . ".php")?>">
This might not be the best way to achieve what I was aiming for, but it works.
Upvotes: 1
Reputation: 37904
what about setting already submitted
to true into session if user submits the respective step. then you can check first in session, if this is true, it means user already submitted this step.
http://www.w3schools.com/php/php_sessions.asp here is very good session guidance
Upvotes: 0