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

Avatar of undefined
Last Comment
JefferyBraun

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Gary

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
JefferyBraun

ASKER
Thank you.... That did work, but why did setting my vars to blank not work?
Gary

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

Ray Paseur

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

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
JefferyBraun

ASKER
Thank You very much.
Your explanation made it clear.

Thanks