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> 
3
  • use unset($_POST['value']); Commented Jan 10, 2013 at 7:58
  • 1
    This post will help you stackoverflow.com/questions/11765144/… Commented Jan 10, 2013 at 7: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. Commented Jan 10, 2013 at 7:59

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

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(); } 

3 Comments

after refreshing it is giving error "Notice: Undefined index: time"
it is not even printing name after using header
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.
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.

Comments

-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> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.