We help IT Professionals succeed at work.

php question

JefferyBraun
JefferyBraun asked
on
386 Views
Last Modified: 2014-05-20
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!
Comment
Watch Question

CERTIFIED EXPERT
Expert of the Year 2014
Top Expert 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank you.... That did work, but why did setting my vars to blank not work?
CERTIFIED EXPERT
Expert of the Year 2014
Top Expert 2014

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

Most Valuable Expert 2011
Author of the Year 2014

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

Author

Commented:
Thank You very much.
Your explanation made it clear.

Thanks
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.