skip to Main Content

I have a piece of PHP code, and I’m encountering a Confirm form resubmission error when I click the ‘Back’ button in my browser within my development environment. I’ve included the relevant code snippet below for reference:

if (!isset($_GET['auditors']) || isset($_GET['reshape'])) { /* 1st if block STARTS */
    

    if (!isset($_GET['auditor_type']) || 'INFO' == $_GET['auditor_type']) {
       
        if (empty($_GET['auditor_type'])) {
       
       }
    } else {
        if (ValidateType($_GET['auditor_type'], $auditor_types)) {
        } 
        else {

        }
    } ?>
    <head>
        <title>Hello World</title>
    </head>
    <body>
    <h1>Hello World Report</h1>

    <?php
    <form method="get" action="">

        <fieldset>
            <legend>Auditors</legend>

            <?php

            foreach ($auditors as $id => $auditor) {
                
            } ?>

        </fieldset>

        <div class="submit">
            <input type="submit" name="report_maker" value="Report"/>
        </div>

        <fieldset>
            <legend>Auditors Selected</legend>

            <?php

            foreach ($auditor_types as $type) {
                
            } ?>

        </fieldset>

        <div class="submit">
            <input type="submit" name="reshape" value="Auditors list"/>
        </div>

    </form>

    </body>
    </html>
    <?php
} /* 1st if block ENDS */

Upon clicking the ‘Back’ button, the code above is executed. During debugging, I noticed that all GET variables are unset, causing it to enter the if block.

Problem Statement: After clicking ‘Back’ button, I received a Confirm form resubmission error. I’m looking for guidance on how to modify the code to resolve this issue.

Expected Result: When clicking Back button, the page should display the following headings and content inside the form:

Headings:

  1. Title: Hello World
  2. Heading: Hello World Report
  3. Fieldset: Auditors
    • Legend: Auditors
    • (Content generated by PHP loop, as shown in the code)
  4. Fieldset: Auditors Selected
    • Legend: Auditors Selected
    • (Content generated by PHP loop, as shown in the code)

Form Content:

The form should include all the inputs and elements as defined in the code snippet.

2

Answers


  1. Clicking the back button does not reload the page, it resends the previous request. So, if the previous request was a form submission, it will try to resubmit the form. It’s that simple.

    Login or Signup to reply.
  2. Simple workaround is to redirect the user on other page upon form submission.

    Or you can display a confirmation page on a new tab.

    Don’t rely on back button because it will resubmit the form.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search