1

I have created a muilti-page PHP form that inserts the results into a MySQl database and works fine, the only issue I have is when I try to go back I get the 'Confirm Form Resubmission''ERR_CACHE_MISS' error page.

Php isn't my strongest language and need a bit of help with fixing it.

its a 20 page survey with each page having radio buttons with a choice of 1 to 10.

Sample of my code below, any advice would be great.

Cheers

<?php session_start();
$_SESSION['answer_04'] = $_POST['answer_04'];
?>

<form action="peem-05.php" id="peem" method="post">

<ul class="answers">
    <li class="group1"><input type="radio" id="radio1" name="answer_05" value="1" data-msg-required="Please choose an answer bewteen 1 and 10" required><label for="radio1"><br>1</label></li>
    <li class="group1"><input type="radio" id="radio2" name="answer_05" value="2" required><label for="radio2"><br>2</label></li>
    <li class="group2"><input type="radio" id="radio3" name="answer_05" value="3" required><label for="radio3"><br>3</label></li>
    <li class="group2"><input type="radio" id="radio4" name="answer_05" value="4" required><label for="radio4"><br>4</label></li>
    <li class="group3"><input type="radio" id="radio5" name="answer_05" value="5" required><label for="radio5"><br>5</label></li>
    <li class="group3"><input type="radio" id="radio6" name="answer_05" value="6" required><label for="radio6"><br>6</label></li>
    <li class="group4"><input type="radio" id="radio7" name="answer_05" value="7" required><label for="radio7"><br>7</label></li>
    <li class="group4"><input type="radio" id="radio8" name="answer_05" value="8" required><label for="radio8"><br>8</label></li>
    <li class="group5"><input type="radio" id="radio9" name="answer_05" value="9" required><label for="radio9"><br>9</label></li>
    <li class="group5"><input type="radio" id="radio10" name="answer_05" value="10" required><label for="radio10"><br>10</label></li>
</ul>


<a href="javascript:history.back(1)"><img class="backButton" src="img/back-button.png"/></a>

<button class="rightArrow" name="submit" type="submit" value="Next" ><img class="backButton" src="img/next-button.png"/></button>
</form>
Phil
  • 157
  • 1
  • 1
  • 13
  • 1
    Do not use history to go back. Better save all info to session and use some UID to identify form. Going back means re-visiting same page instead of going back in browser history. I think browser may delete your form page cache after you submit form. – Justinas May 30 '16 at 07:26
  • Hi Justinas, thanks for the reply. I dont suppose you could point me in the direction of the correct code to use? – Phil May 30 '16 at 07:27
  • 1
    Possible duplicate of [navigate back with PHP form submission](http://stackoverflow.com/questions/19215637/navigate-back-with-php-form-submission) – Netham May 30 '16 at 07:27
  • please go through the link it will helps you. [Stackoverflow answer](http://stackoverflow.com/questions/3923904/preventing-form-resubmission) – anuraj May 30 '16 at 07:29
  • Thanks for the feedback everyone, this has been enormously helpful – Phil May 30 '16 at 20:56

2 Answers2

0

Instead of using javascript:history.back(1) you better save data to PHP Session and going back means re-visiting same page.

[save.php]

$step = $_POST['formStep'];
$formData = $_POST['Form'][$step]; // all values from form
$formID = $_POST['formID']; // validate

if ($formData && $step) {
    if ($step < $maxSteps) {
        $_SESSION[$formID][$step] = $formData;

        $this->showStep($step++);
    } else {
        $this->db->saveForm($_SESSION[$formID]);
    }
}

[showStep.php]

$step = isset($_REQUEST['step']) ? $_REQUEST['step'] : 1;
$formID = isset($_SESSION['formID']) ? $_SESSION['formID'] : rand(0, 99);
$formData = isset($_SESSION[$formID][$step]) ? $_SESSION[$formID][$step] : [];
?>
<form action="save.php?step=<?= $step; ?>">
    <input type="hidden" value="<?= $formID; ?>" name="formID"/>
    <?php switch ($step): ?>
<?php case 1: ?>
      <input type="radio" id="radio10" name="Form[<?= $formID; ?>][<?= $step; ?>][answer_05]" value="10" <?= isset($formData['answer_05']) ? 'checked="checked"' : ''; ?>/>
<?php case 20: ?>
      ...
<?php break; ?>
</form>
<a href="showStep.php?step=<?= $step - 1; ?>">Back</a>
Justinas
  • 41,402
  • 5
  • 66
  • 96
0

Note that you have to rename your peem-x.php files removing the zero to make that code work. I commented my changes, maybe this helps you.

<?php
session_start();

$page = basename(__FILE__); // get current filename
$page = preg_replace("/[^0-9]/","",$page); // remove everything except numbers
$page_last = "peem-".($page-1).""; // calculate last page name
$page_now = "peem-".$page.""; // current page name

for ($i = 0; $i < count($_POST['answer']); $i++) { // loop for total count of answers selected
    $_SESSION['page'.$page_now.'']['answer'.$i.''] = $_POST['answer'.$i.''];  // assign session to answer
}
?>

<form action="" id="peem" method="post">

    <ul class="answers">
        <li>Please choose an answer between 1 and 10</li>
        <?php
        for ($i = 1; $i < 10; $i++) {
            // check every loop if it's the current page and current answer that has been checked before
            // if so, set a variable to mark it as checked
            if ($_SESSION['page'] == $page_now && $_SESSION['page']['answer'.$i.''] == 'page'.$page_now.'answer'.$i.'')
            {
                $checked = " checked"; // create var to check current answer in loop
            }

            echo '<li class="group1"><input type="radio" id="radio1" name="answer' . $i . '" value="1"'.$checked.'><label for="radio' . $i . '"><br>' . $i . '</label></li>';
            unset($checked); // unset $checked var for next loop to avoid checking unchecked radio buttons
        }
        ?>
    </ul>


    <a href="-<?php echo $page_last; ?>.php"><img class="backButton" src="img/back-button.png"/></a>

    <button class="rightArrow" name="submit" type="submit" value="Next" ><img class="backButton" src="img/next-button.png"/></button>
</form>

<?php
// test session and post variables
echo "<pre>";
print_r($_POST);
print_r($_SESSION);
echo "</pre>";
?>
AlexioVay
  • 4,338
  • 2
  • 31
  • 49
  • Hi Vaia, thanks for your code update but when I go back a page I don't get an error but the radio button I selected isnt checked any more. Any ideas? – Phil May 30 '16 at 21:31
  • That is because you just can get the data from the last form $_POST data that has been submitted (next button). So you have to check your $_SESSION variables where you saved all answers for that specific page and mark every answer checked that's in the variable. In that case I would also save the current page you're on right now in the variable along with the answers that have been checked. Can you follow me and try it on your own or do you need help? – AlexioVay May 30 '16 at 21:40
  • oh ok thank, sorry my PHP knowledge isn't great. I will give it a try shortly and let you know how I go. Cheers – Phil May 30 '16 at 23:02
  • @Phil Hey, so I edited my answer that should check answers that has been checked before by the user. I saved the current page in an multi-dimensional array, so the system knows where the user is right now. I also commented the lines to help you understand it better. If I helped you, I'd appreciate marking my answer as correct. – AlexioVay May 31 '16 at 06:20