-1

I am working on a html/php code as shown below in which I want to call a particular section of php code on click of a button.

<html> 
    <?php 
     if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['go-button'])) {
     for each  ($mp4_files as $f) {

     }
    }
    ?>

  <form action ="" method="POST">
    <table>
       <td style="width:5%; text-align:center;"><button style="width:90px;" type="submit" name="go-button" value="Go"  class="btn btn-outline-primary">Go</button>  <!-- Line#B -->
   </table
  </form>
</html>

On click of a button at Line#B, the php code is call above and it goes inside the if block.

The issue which I am having right now is that on refresh of a page, it is also going inside the if block which I don't want to happen. I want if block to be active only on click of Go button.

Problem Statement:

I am wondering what changes I should make in the php code above so that on refresh of a page it doesn't go inside the if block. It should go only on click of a button at Line#B.

flash
  • 1,455
  • 11
  • 61
  • 132
  • When you refresh the page, doesn't it ask if you want to post the data again? – MER Jun 04 '19 at 14:23
  • 2
    Possible duplicate of [Best way to avoid the submit due to a refresh of the page](https://stackoverflow.com/questions/5690541/best-way-to-avoid-the-submit-due-to-a-refresh-of-the-page) – Epodax Jun 04 '19 at 14:24
  • On refresh of a page, it doesn't give any warning/alert. – flash Jun 04 '19 at 14:24
  • @Epodax Its the possible duplicate of [this](https://stackoverflow.com/questions/6833914/how-to-prevent-the-confirm-form-resubmission-dialog) as well but it didn't help much. – flash Jun 04 '19 at 14:26
  • Why doesn't the idea of redirecting to a different page after you complete the action help? If you redirect to a different page and they refresh the page, nothing will happen. – MER Jun 04 '19 at 14:33
  • Not an answer to your question but it looks like your HTML is invalid. You cannot split table elements as you have above. The form MUST wholly contain the entire table or the form must be wholly contained in a single table cell – Professor Abronsius Jun 04 '19 at 14:34
  • @RamRaider Thats just a snippet. – flash Jun 04 '19 at 14:35
  • Something like this `if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['go-button'])) { header("Location:www.xyz.com"); }` – flash Jun 04 '19 at 14:41
  • does that block of PHP generate any output? If you are not using `output buffering` can it be located at the top of the page rather than within the document? You should be able to use `header('Location: page.php')` to ensure that after processing the POST request the page cannot be re-submitted on page reload – Professor Abronsius Jun 04 '19 at 14:43
  • Another idea is that you could possibly use Ajax and FormData if you don't have to support old browsers and then there are no refresh issues at all. – MER Jun 04 '19 at 14:46

1 Answers1

2

I'm not sure where you were having issues as such - in one comment you virtually hit the nail on the head as it were with how to prevent the form being re-submitted if the user reloads the page. You ought to be able to adopt an approach like the following - though if the PHP does generate content and it located within the document body somewhere you'd need to use output buffering to prevent errors regarding headers already sent

<?php 
    if( $_SERVER['REQUEST_METHOD']=='POST' and !empty( $_POST['go-button'] ) ) {

        foreach( $mp4_files as $f ) {
            /* do stuff */
        }

        /* finished processing POST request, redirect to prevent auto re-submission*/
        exit( header( sprintf('Location: %s', $_SERVER['SCRIPT_NAME'] ) ) );
    }
?>

<html>
    <head>
        <title></title>
    </head>
    <body>
        <form method="POST">
            <table>
                <tr>
                    <td style="width:5%; text-align:center;">
                        <!-- Line#B -->
                        <button style="width:90px;" type="submit" name="go-button" value="Go" class="btn btn-outline-primary">Go</button>
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • What is 'SCRIPT_NAME' ? – flash Jun 04 '19 at 15:03
  • the name of the script/page being run - ie: the one with the form – Professor Abronsius Jun 04 '19 at 15:03
  • the one with the form ? I don't think so I have given any name to the form. – flash Jun 04 '19 at 15:07
  • no... it refers to the actual page NOT the form! It provides an easy way to redirect back to the same page without having to know the name of the page – Professor Abronsius Jun 04 '19 at 15:09
  • It means '%s' and 'SCRIPT_NAME' would be same ? – flash Jun 04 '19 at 15:15
  • in the above `%s` is a placeholder for a STRING value which happens to be `$_SERVER['SCRIPT_NAME']`. – Professor Abronsius Jun 04 '19 at 15:18
  • something like this ? exit( header( sprintf('Location: %s', $_SERVER['http://abc-xyz/status.php'] ) ) ); ? – flash Jun 04 '19 at 15:20
  • No, leave the `SCRIPT_NAME` text as-is. That is a global server variable name. See [here](https://www.php.net/manual/en/reserved.variables.server.php). It will automatically be filled with the PHP file name being run. So, this code will redirect to itself to remove the POST issue on refresh. – MER Jun 04 '19 at 15:23
  • Ok I understand, so it should be in this way exit( header( sprintf('Location: http://abc-xyz/status.php', $_SERVER['SCRIPT_NAME'] ) ) ); ? – flash Jun 04 '19 at 15:26
  • `abc-xyz/status.php` is the page where my html/php code is running. – flash Jun 04 '19 at 15:28
  • 1
    I am sorry for the confusion. I understand now, I don't have to change anything here `exit( header( sprintf('Location: %s', $_SERVER['SCRIPT_NAME'] ) ) );` I am sorry again. – flash Jun 04 '19 at 15:36
  • @RamRaider I have one more question. Its not [similar](https://stackoverflow.com/questions/56467383/how-to-make-a-html-button-target-a-specific-array-index-in-php/56480251#56480251). I am wondering if you can have a look. – flash Jun 06 '19 at 20:12