Link to home
Start Free TrialLog in
Avatar of JefferyBraun
JefferyBraun

asked on

php question

I have a simple page that uses php.
It basically a simple form with two input boxes and a submit button.
I enter the info, click the submit button, but if I refresh the page, the data input before is reposted. How do I clear the data after the submit.

Here is my php code. (which does not work).

<?php
if($_POST){
      $name = $_POST['commenterName'];
      $message = $_POST['comment'];
      $error = 0;
      
      if(empty($name)){
            $error = 1;
            echo "Please enter your name.";
      }
      if(empty($message)){
            $error = 1;
            echo "Please enter your message.";
      }
      if($error==0){
            $handle = fopen("comments.html","a");
            $messageToWrite = $name . ":<br/>" . $message . "<br/><br/>";
            fwrite($handle,$messageToWrite);
            fclose($handle);
            $name=null;
            $message=null;
            $messageToWrite = null;
      }
}
?>

Thank You!
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JefferyBraun
JefferyBraun

ASKER

Thank you.... That did work, but why did setting my vars to blank not work?
There is no point to setting them to null, you are leaving the page, just remove those lines
Also you should add this after the header line
exit;

      if($error==0){
             $handle = fopen("comments.html","a");
             $messageToWrite = $name . ":<br/>" . $message . "<br/><br/>";
             fwrite($handle,$messageToWrite);
             fclose($handle);
             header('Location: thispage.php');
             exit;
       }

Open in new window

if I refresh the page, the data input before is reposted
The browser should warn about that.  It's possible to detect multiple submissions of the same data.  This shows the principles in action.

<?php // prevent_multi_submit.php
error_reporting(E_ALL);

/**
 * PREVENT REPEATED DATA SUBMISSIONS DUE TO BROWSER REFRESH
 * RE-CLICK ON SUBMIT, OR BROWSER BACK BUTTON
 *
 * GET-METHOD REQUESTS SHOULD BE IDEMPOTENT AND (IN THEORY)
 * WOULD NOT DISRUPT THE DATA MODEL  THIS FUNCTION CAN TEST
 * EITHER $_GET OR $_POST REQUEST VARIABLES
 *
 * EXAMPLE:
 *    if ( multi_submit() )
 *    {
 *       handle error
 *    }
 *    else
 *    {
 *       normal processing
 *    }
 */


// ALWAYS START THE PHP SESSION ON EVERY PAGE
session_start();


// A FUNCTION TO RETURN TRUE OR FALSE ABOUT MULTI-SUBMIT CONDITIONS
function multi_submit($type="POST")
{
    // MAKE THE FUNCTION WORK FOR EITHER GET OR POST SUBMITS
    $input_array = (strtoupper($type) == "GET") ? $_GET : $_POST;

    // GATHER THE CONTENTS OF THE SUBMITTED FIELDS AND MAKE A MESSAGE DIGEST
    $string = NULL;
    foreach ($input_array as $val)
    {
        $string .= $val;
    }
    $string = md5($string);

    // IF THE SESSION VARIABLE IS NOT SET THIS IS NOT A MULTI-SUBMIT
    if (!isset($_SESSION["multi_submit"]))
    {
        $_SESSION['multi_submit'] = $string;
        return FALSE;
    }

    // IF THE SESSION DATA MATCHES THE MESSAGE DIGEST THIS IS A MULTI-SUBMIT
    if ($_SESSION['multi_submit'] == $string)
    {
        return TRUE;
    }
    else
    {
        $_SESSION['multi_submit'] = $string;
        return FALSE;
    }
}


// SHOW HOW TO USE THE FUNCTION
if (!empty($_POST))
{
    if (multi_submit())
    {
        echo "ALREADY GOT THAT";
    }
}


// CREATE THE FORM FOR THE DEMONSTRATION
$form = <<<FORM
<form method="post">
ENTER SOMETHING, THEN REENTER IT
<input name="mydata" />
<input type="submit" />
</form>
FORM;

echo $form;

Open in new window

Thank You very much.
Your explanation made it clear.

Thanks