1

I've got a form with 50 questions. After the user has filled in the form, I want to take them to another page to first cancel / confirm that they are finished with the form. My question is if they select cancel, how do I put back all the fields that has been answered in the form?

EDITED

I've edited my question to show my code because I'm strugling:

$sql1="SELECT * FROM ex_question WHERE test_name = '$tid' ORDER BY RAND()";
$result1=mysql_query($sql1);
echo "<form method='post' action='....'>";
while($row1 = mysql_fetch_array($result1))
{
 $test_name=$row1['test_name'];
 $q_nr=$row1['q_nr'];
 $q_type=$row1['q_type'];
 $question=$row1['question'];
 $option1=$row1['option1'];
 $option2=$row1['option2'];
 $option3=$row1['option3'];
echo "$question";
if ($q_type != 'mr') {
if($option1!="") {
echo "<input type='radio' name='question[$q_nr]' value='A'>$option1<BR>";
} else {
echo ''; }
if($option2!="") {
echo "<input type='radio' name='question[$q_nr]' value='B'>$option2<BR>";
} else {
echo ''; }
if($option3!="") {
echo "<input type='radio' name='question[$q_nr]' value='C'>$option3<BR>";
} else {
echo ''; }
} else { // else if not <> mr
if($option1!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='A'>$option1<BR>";
} else {
echo ''; } 
if($option2!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='B'>$option2<BR>";
} else {
echo ''; } 
if($option3!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='C'>$option3<BR>";
} else {
echo ''; } 
} //end else if q_type <> mr
echo "<BR>";
echo "<BR>";
echo "</p>";
} //end while row1
echo "<input type='submit' value='Finish'>";
echo "</form>";
asked Nov 21, 2011 at 9:52

4 Answers 4

1

Make the Cancel button a Submit and let it post back to the original questionnaire. In there, you can do like this:

<input type="text" name="q1" value="<?=isset($_POST['q1']) ? $_POST['q1'] : '';?>" />

Or:

<input type="radio" name="q2" value="A" <?=isset($_POST['q2']) && $_POST['q2'] == 'A' ? 'checked="checked"' : '';?>" />
<input type="radio" name="q2" value="B" <?=isset($_POST['q2']) && $_POST['q2'] == 'B' ? 'checked="checked"' : '';?>" />

Same goes for option elements inside a select (selected="selected").

answered Nov 21, 2011 at 9:55
Sign up to request clarification or add additional context in comments.

4 Comments

Can you please be so kind and have a look at my edited question
I'm sorry but I don't see any additional question I can answer?
My apologies, I am trying to implement your code on my original but I can't get it to work, it looks like this echo "<input type='radio' name='question[$q_nr]' value='A'=isset($_POST['question[$q_nr]']) && $_POST['question[$q_nr]'] == 'A' ? 'checked='checked'' : '';' />";
"On servers with shorthand support enabled you can start a scripting block with <? and end with ?>. For maximum compatibility, we recommend that you use the standard form (<?php) rather than the shorthand form."
1

Pass the $_POST or $_GET array to the confirmation page, and if they hit cancel, pass the array back to the page, and fill in the elements.

answered Nov 21, 2011 at 9:54

Comments

1

The easiest solution would be adding:

<INPUT type=button value="Cancel" onClick="history.back();">

When you go back, the form would still be there.

answered Nov 21, 2011 at 9:55

Comments

0

You can just use $_REQUEST['Answer'] in value tag of form.

Justin
87.3k49 gold badges231 silver badges374 bronze badges
answered Nov 21, 2011 at 10:05

1 Comment

Hi Justin is there is a problem with my comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.