0

every time i am refreshing the page and i am getting the same value stored in the post array. i want execution of echo statement only after submit and after refreshing no echo results..

<?php
if(isset($_POST['submit'])) 
{ 
$name = $_POST['name'];
echo "User name : <b> $name </b>";


}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
srs
  • 68
  • 1
  • 1
  • 9
  • use unset($_POST['value']); – laxonline Jan 10 '13 at 07:58
  • 1
    This post will help you http://stackoverflow.com/questions/11765144/unset-post-variables-after-form-submission – Edwin Alex Jan 10 '13 at 07:58
  • That is expected behaviour. If you refresh a POSTed form then it will do that. However if you want data to show only once per submit you can store a session variable and run a check on that. – Hanky Panky Jan 10 '13 at 07:59

4 Answers4

2

From just a form, you won't be able to check if it was a refresh, or a first submit, regardless of using GET or POST method.

To ensure a single message, you need to:

a. redirect the user to somewhere else after you processed the request.

if(isset($_POST['submit'])) {
  // process data
  header("Location: new-url");
}

And display the message on the other URL.

b. set a cookie / session variable, which tells you the form was already processed.

if(isset($_POST['submit']) && !isset($_SESSION['form_processed'])) {
  $_SESSION['form_processed'] = true;
}

This second approach will kill your form until the user closes the browser, so you should do something more complex - like storing another hidden field in the form, and storing that in the session.

Dutow
  • 5,638
  • 1
  • 30
  • 40
1

If you submit a form and then refresh the resulting page, the browser will re-post the form (usually prompts first). That is why the POST data is always present.

An option would be to store a session variable and have it sent in the form, then check if it matches in the form processing code - to determine if it is a re-post or not.

Within the form:

<input type="hidden" name="time" value="<?php echo $time; ?>" />

In the PHP:

session_start();

if(isset($_POST['submit'])) 
{
    if(isset($_SESSION['time']) && $_SESSION['time'] == $_POST['time'])
    {
        echo "User name : <b> $name </b>";
    }
}

$time = $_SESSION['time'] = time();

Another option is to redirect after processing the post data:

if(isset($_POST['submit'])) 
{
    ...
    ...

    header('Location: ' . basename($_SERVER['PHP_SELF']));
    exit();
}
MrCode
  • 63,975
  • 10
  • 90
  • 112
  • after refreshing it is giving error "Notice: Undefined index: time" – srs Jan 10 '13 at 08:22
  • it is not even printing name after using header – srs Jan 10 '13 at 08:23
  • I've added an `isset()` to prevent the `Notice`. The session option is better than the redirect because you can't print anything before a redirect. – MrCode Jan 10 '13 at 08:39
0

You need to maintain a state as to whether $name has already been displayed or not. The easiest way is probably to maintain that state in a browser cookie.

<?php
$nonce = $_COOKIE['nonce'];
$new_nonce = mt_rand();
setcookie('nonce', $new_nonce);
if(isset($_POST['submit']) && $_POST['nonce'] == $nonce) 
{ 
    $name = $_POST['name'];
    echo "User name : <b> $name </b>";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="nonce" value="<?php echo $new_nonce ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>

Problems

  • you are polluting the user “session” with stale variable.
  • this will break if your user opens several windows (or tabs) to the same page. To fix this you would have to change the nonce cookie into an array of nonces, and update it accordingly.
kmkaplan
  • 18,655
  • 4
  • 51
  • 65
-1

if you want refresh page after submit use

<form method="get"

sure if your form hasn't a lot of data and also need to use $_GET instead of $_POST variable:) correct way for you, but this logic is not good, need to refactor this script:

<?php
if(isset($_POST['submit'])) 
{ 
$name = $_POST['name'];
echo "User name : <b> $name </b>";
unset($_POST['submit']);
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
Nick
  • 602
  • 9
  • 22