Reputation: 1860
I've got a page with 50 questions, then when the user is finished and submits the form I take the user to a confirmation page where the user can choose to go back to the questions page or to finalize. If the user finalizes it goes to another page where the pass rate is worked out. My problem is the #_POST foreach does not carry over to the final page. Is there a way that I can attach this to a session or must I write it to a table and then get it from there again?
Question page
echo "<input type='checkbox' name='question[$q_nr][]' value='A'>$option1<BR>";
echo "<input type='checkbox' name='question[$q_nr][]' value='B'>$option2<BR>";
Confirmation page
foreach($_POST['question'] as $key => $ans)
{ ..... }
Final page which works out the percentages and where the problem is it does not retrieve from the previous page
foreach($_POST['question'] as $key => $ans)
{ ..... }
Upvotes: 1
Views: 414
Reputation: 14863
Have you simply tired
$_SESSION[] = $_POST
?
EDIT:
$_SESSION[] = $_POST;
or
foreach ($_POST as $k => $v) {
$_SESSION[$k] = $v;
}
Third page:
foreach ($_SESSION as $k => $v) {
...
}
Upvotes: 2
Reputation: 3960
If i may suggest,
instead of posting from questions page to confirmation page, when the user finalizes the questions just hide the html element of the questions and open a new one and fill it with the confirmation data.
Then you have two buttons, go back to questions and post the form. If user clicks "go back" just toggle questions/confirmation html elements, if the user clicks "post", it will actually submit the form data.
Upvotes: 0
Reputation: 8001
It's either you use sessions to store the post data or you work out the result each time one gets to the confirmation page. You can then transfer the result to the final page via an encoded GET parameter
Upvotes: 0
Reputation: 2269
Yes, there is a way to attach $_POST
to a session, since it is a variable.
And yes, you can write a table and get from there again.
What's better? It depends on what you expect your users do and provide proper application behaviour.
For example, if they are evil and never finalize and you write a lot of useless data in a table if you don't provide cleaning mechanisms.
Upvotes: 1