0

I have a very simple PHP code that is supposed to output "Hi" as soon as the button is pressed. It works perfectly fine the first time I open the page - there is only one button on the page, I press it and get "Hi" The problem comes when I try to refresh the page. The "Hi" does not disappear.

I tried several browsers and refreshed pages without using cash. This doesn't help. The only thing that helps is opening the page in a new tab. Then there is no "Hi" - until I press the button for the first time.

<form method="post">
    <input type="submit" name="test" id="test" value="RUN" /><br/>
</form>

<?php

function testfun()
{
echo 'Hi ';
}


if(array_key_exists('test',$_POST)){
   testfun();
}

?>
user15933
  • 11
  • 5
  • 3
    You need to clear the `$_POST` array. Refreshing the page is resubmitting the form. – Jay Blanchard Mar 25 '19 at 19:32
  • To demonstrate, after you post the form press your browser's "reload" button to reload the page. Observe the results. Then after you post the form, select the address in the browser and press return. Observe the results. Consider the potential difference between these two actions. – David Mar 25 '19 at 19:36
  • Look into [PRG](https://en.wikipedia.org/wiki/Post/Redirect/Get) to avoid this kind of behavior. – Jeto Mar 25 '19 at 19:50
  • I tried to clear $_POST array in several ways (all of them adding this code after I run testfun()): 1. $_POST = array(); 2. unset($_POST); 3. foreach ($_POST as $key => $value) { $_POST[$key] = NULL; } } – user15933 Mar 25 '19 at 20:11
  • The methods I gave above work for IE, but do not work for Firefox and Chrome. I also tried the method proposed by Rafael Guimarães with the same result - doesn't work in Firefox and Chrome. At the same time, in the Firefox and Chrome if I open the page again (place cursor in the address line and press Enter) "Hi" is not there, as it should be. But if I just refresh, "Hi" remains. Is there a way to make it work for all browsers? – user15933 Mar 25 '19 at 20:17

3 Answers3

1

put correct action page, or empty to same url(but put the tag anyway).

<form method="post" action="page_here.php">
    <input type="submit" name="test" id="test" value="RUN" /><br/>
</form>

try check with isset().

function testfun() { echo 'Hi '; }

if ( isset($_POST) && isset($_POST['test']) ) {
    testfun();
}
  • This code is on the wordpress page. But I put it on the completely blank page as well - with the same result. "Hi" remains after refreshing in all browsers except fo IE. – user15933 Mar 25 '19 at 20:34
0

See Post/Redirect/Get. You can't display the confirmation message right after submitting, otherwise refreshing the browser will always send the exact same POST.

Instead:

  • when the form is submitted, save the state into the session, then make a redirect using header,
  • on form display, check that the session has said state, if so display your message then clear the state from the session.

Sample below:

<?php
session_start();

if (array_key_exists('test', $_POST)) {
  $_SESSION['posted'] = true;
  // header requires an absolute URL so recreating it from $_SERVER superglobal
  header('Location: ' . (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
  die;
}
?>

<form method="post">
  <input type="submit" name="test" id="test" value="RUN">
</form>

<?php
function testfun()
{
  echo 'Hi';
}

if ($_SESSION['posted'] ?? null === true) {
  testfun();
  unset($_SESSION['posted']);
}
Jeto
  • 14,596
  • 2
  • 32
  • 46
  • Thank you, Jeto! This works on a blank page. But what I'm actually doing is pasting this piece of code into wordpress site, in this case it does not work. What modifications I can bring in order to make it work in the wordpress? – user15933 Mar 25 '19 at 22:37
  • You should tag your question with Wordpress in this case, and explain your problem more specificially around that. I'm not very familiar with it, but I suppose the location you're redirecting to must be adjusted somehow. What exactly is happening? ("It does not work" is a bit vague :)) – Jeto Mar 26 '19 at 03:52
  • It just doesn't show the button at all. Wordpress has its own rules for working with sessions and I'm not even sure if it is possible to do something with the header. Is there any way to circumvent this? Could you explain what the command with header does? – user15933 Mar 27 '19 at 15:45
  • Also, I'm not sure why just unset($_POST); does not work. If I unset the $_POST right after going into testfun() then there is nothing in $_POST anymore - how the value (button pressed) is in $_POST after the page refreshing? – user15933 Mar 27 '19 at 15:48
  • @user15933 There's a link to the docs for `header` in my answer, basically it sends a HTTP header to the response, here a `Location` header is a redirection to the specified URL. The reason `unset($_POST)` doesn't work is because it doesn't matter. Refreshing your browser after a POST just sends the POST request again with the same data, so it's there again on the receiving end. – Jeto Mar 27 '19 at 16:06
  • It seems that there is pretty elegant solution with JS here https://stackoverflow.com/questions/6320113/how-to-prevent-form-resubmission-when-page-is-refreshed-f5-ctrlr Just No need in playing with sessions and headers, which might be a problem for WP – user15933 Mar 28 '19 at 15:28
-1

I don't think you understand how posting works, when you post data you're sending it to a page, the data only lives as long as that page does. That said when you reload the page that data will disappear as it's volatile. If you want to persist the data you need to store it in a session.

Also please note that when you refresh a page it resubmits a form, where if you click on the address bar and hit enter you will get a blank slate!

Please look into sessions, it will resolve your issue: PHP Manual

Lulceltech
  • 1,662
  • 11
  • 22