Reputation: 1025
I am currently building a multiple pages form using $_SESSION
, everything has been working fine so far. Yet, even though I am able to retrieve all the data at the end of the 6 pages, it seems that it is not possible to go back in the form if the user made a mistake.
Let's say he is at page 5 and he realizes that he needs to go back to page 3 to change something, he won't be able to do so. Firefox gives this error message :
Document Expired This document is no longer available. The requested document is not available in Firefox's cache.As a security precaution, Firefox does not automatically re-request sensitive documents.Click Try Again to re-request the document from the website.
This is what my forms look like, this one is Form-1.php :
<form action="Form-2.php" method="post">
<fieldset>
<legend>Information</legend>
<p>
<label for="name">Name</label>
<input id="name" type="text" name="name" required/>
</p>
<p>
<label for="age">Age</label>
<input id="age" type="number" name="age" placeholder="Ex. 1988" required/>
</p>
<p>
<input id="sex-m" type="radio" name="sex" value="Male" required/>
<label for="sex-m">Male</label>
<input id="sex-f" type="radio" name="sex" value="Female" required/>
<label for="sex-f">Female</label>
</p>
</fieldset>
<input type="submit" value="Next page"/>
</form>
I am using SESSION
to retrieve data from the previous form, this one is from Form-2.php :
session_start();
$_SESSION['name'] = $_POST['name'];
$_SESSION['age'] = $_POST['age'];
$_SESSION['sex'] = $_POST['sex'];
And so on for the six pages.
It works fine, it retrieves all the values correctly, but it is not possible to go back when filling the form.
What am I doing wrong? How can I make sure the user will be able to go back if he makes a mistake?
Thanks for your help.
EDIT :
I ended up with the following code, solving half of my problem. Still from Form-2.php :
session_start();
if (isset($_SESSION['location']) && isset($_SESSION['job'])) {
$location = $_SESSION['location'];
$job = $_SESSION['job'];
} else {
$_SESSION['name'] = $_POST['name'];
$_SESSION['age'] = $_POST['age'];
$_SESSION['sex'] = $_POST['sex'];
$location = '';
$job = '';
}
Then I simply display the values in the fields with :
If text : <?php echo $location; ?>
If radio : <?php if(isset($job) && $job == "Yes"){echo "checked=\"checked\" ";} ?>
It works quite well, the different forms - if the values have been set - are pre-populated just fine.
My problem is, as you can see, if I go back and change a value, it won't be taken into consideration as I am ignoring POST
if there is a corresponding value stored in SESSION
.
I can't figure out how to make this work properly... Any hint?
Upvotes: 2
Views: 2759
Reputation: 41
djot is right, you need a << back >> and << forward >> button, what you can do in functionality is if back button is clicked pass the values of the page/form as hidden input fields and populate the fields using the values in the hidden..
Upvotes: 0
Reputation: 2947
Not the best solution, but does the job: Do not use back buttons, provide your own forward, backwards, "step" buttons. Then populate the form with the data stored in e.g. the session.
Upvotes: 1