Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to clear $_POST values?

Status
Not open for further replies.

JediDan

Programmer
May 15, 2002
128
US
Hello,
I have a form that submits via POST, which works fine (inserting into a DB). If the user hits Refresh on the page, I would like to not have the data insert a duplicate record. How can I clear the $_POST variables?

unset($_POST) does not work, and I'd rather not have to redirect them to another page just to do this.

Any help is appreciated.

Thanks
 
well if you do something like this.

if (isset($_POST['yourvar'])
{
if ($_POST['yourvar'] != "empty") //empty can be anything
{
// here you do your adding to database stuff
$_POST = "empty";
}
}


tank you,

(>" "<)
(='o'=)
-(,,)-(,,)------------------------------
LORD_GARFIELD
---------------------------------------
 
If they Reload the page, the browser will send exactly the same POST vars as they sent last time---no matter what you do with the value of $_POST. You can't modify this on the server-side, because the behavior of the browser is beyond your control.

and I'd rather not have to redirect them to another page just to do this

Redirects are very easy, two lines of code. You can actually redirect them right back to the same page.

Code:
<?php
header("Location: [URL unfurl="true"]http://www.server.com/mypage.php");[/URL]
exit;
?>

And to put this into the big picture, your script might look like this (I'm pretending it's a feedback form):

Code:
<?php
    if (isset($_REQUEST['name'] && isset($_REQUEST['email']) && isset($_REQUEST['comments'])) {
         mysql_connect( ... );
         ....             // more MySql related stuff omitted
         mysql_query("INSERT INTO `feedback` (`name`,`email`,`comment`) VALUES('".
            mysql_real_escape_string($_REQUEST['name']."','".
            mysql_real_escape_string($_REQUEST['email']."','".
            mysql_real_escape_string($_REQUEST['comments']."'");

         // redirect them back here
         header("Location: [URL unfurl="true"]http://www.mysite.com/feedback.php?action=done");[/URL]
         exit;
    } else {
       ?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Feedback</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>

<body>
<h1>Our Feedback Form</h1>

   [red][b]<?php if ($_REQUEST['a'] == 'done') { ?>[/b][/red]

<p><em>Thank you for your valuable feedback!</em></p>

   [red][b]<?php } else { ?>[/b][/red]

<p>Please enter the fields below.</p>
<form action="feedback.php" method="post" name="feedback" id="feedback">
  <table cellspacing="0">
    <tr>
      <td>Your name:</td>
      <td><input name="name" type="text" id="name"></td>
    </tr>
    <tr>
      <td>E-mail address:</td>
      <td><input name="email" type="text" id="email"></td>
    </tr>
  </table>
  <p><strong>Comments about our site:</strong></p>
  <p> 
    <textarea name="comments" id="comments"></textarea>
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit">
  </p>
</form>

  [red][b]<?php } else { ?>[/b][/red]

</body>
</html>

Just an example!
See how this time, if they hit Reload, there will be no resubmission of variables--All they will get is that "thank you" screen again!

I REALLY hope that helps.
Will
 
apatterno answered your question in context. But to supply the answer to the simple question of how do I clear the $_POST array (or any of the system arrays for that matter)...

Code:
$_POST=array();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top