[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

prevent page refresh from submitting data

I have built a form with the action:

<?php $_SERVER['PHP_SELF'] ?>

I have the content of that form being sent to a DB and the rows from the DB refreshing on the page on each insert using:

onSubmit="reload();"

only problem i have is that if the user refreshes the page, the $_POST variables are still there and will be inserted into the DB as a duplicate.  What can I do to prevent this?
0
axessJosh
Asked:
axessJosh
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
When you do a 'refresh', the browser uses it's history to repeat the action that got it to that page including sending any data to the server.  The most common way to avoid that is to do a redirect in the PHP page to go to a 'new' page which can actually be the same page.  Note that using "header(Location: newpage.php); must be done before any content has been sent.
0
 
Beverley PortlockCommented:
DaveBaldwin said: "Note that using "header(Location: newpage.php); must be done before any content has been sent."

And remember to add an exit; statement immediately AFTER the header or the script will keep executing and if other header statements are processed then they take precedence over this one.

header(Location: newpage.php);
exit;

Open in new window


0
 
axessJoshAuthor Commented:
Where in my code would i put the header location code?

I tried after my insert code, and it took me to that page, but took away the div where i am updating the submitted data.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:

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


// PREVENT MULTIPLE SUBMISSIONS DUE TO REPEATED REFRESH, CLICKS ON SUBMIT BUTTON OR FIRING THE BACK BUTTON
// 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 ALL THE SUBMITTED FIELDS AND MAKE A MESSAGE DIGEST
    $string = NULL;
    foreach ($input_array as $val)
    {
        // CONCATENATE ALL SUBMITTED VALUES
        $string .= $val;
    }
    $string = md5($string);

    // IF THE SESSION VARIABLE IS NOT SET THIS IS NOT A MULTI-SUBMIT
    if (!isset($_SESSION["_multi_submit"]))
    {
        // SAVE THE SUBMITTED DATA MESSAGE DIGEST
        $_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
    {
        // SAVE THE MESSAGE DIGEST TO DETECT FUTURE MULTI-SUBMIT
        $_SESSION['_multi_submit'] = $string;
        return FALSE;
    }
}



// SHOW HOW THIS IS DONE
if (!empty($_POST))
{
    if (multi_submit())
    {
        die("ALREADY GOT THAT");
    }
}



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

echo $form;

Open in new window

0
 
axessJoshAuthor Commented:
Ray,

instead of die("ALREADY GOT THAT"); on a blank page can that error be shown in the DIV where i am reloading the submitted information?
0
 
Ray PaseurCommented:
Almost certainly.  I don't have enough of your code to tell you how to do it, but I am sure it can be done.
0
 
axessJoshAuthor Commented:
Yea, as soon as I hit submit i figured that'd be your answer.

I have implemented and it doesn't resubmit now, but now I've also killed my insert as well...

 
// insert values into the DB
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
// 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 ALL THE SUBMITTED FIELDS AND MAKE A MESSAGE DIGEST
    $string = NULL;
    foreach ($input_array as $val)
    {
        // CONCATENATE ALL SUBMITTED VALUES
        $string .= $val;
    }
    $string = md5($string);

    // IF THE SESSION VARIABLE IS NOT SET THIS IS NOT A MULTI-SUBMIT
    if (!isset($_SESSION["_multi_submit"]))
    {
        // SAVE THE SUBMITTED DATA MESSAGE DIGEST
        $_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
    {
        // SAVE THE MESSAGE DIGEST TO DETECT FUTURE MULTI-SUBMIT
        $_SESSION['_multi_submit'] = $string;
        return FALSE;
    }

$SQL = "INSERT INTO tblContTest (contGrp, contName, contAmnt, contTend, contChkNum, contAcct, contIncLn) VALUES ('1', '$_POST[contributor]' , '$_POST[amount]' , '$_POST[tendType]' , '$_POST[checkNum]' , '$_POST[fund]' , '$_POST[desAcct]')";
		mysql_select_db($database_con_portal_test, $con_portal_test);
  		$Result1 = mysql_query($SQL, $con_portal_test) or die(mysql_error());
		
 }
} // close the post check

// SHOW HOW THIS IS DONE
if (!empty($_POST))
{
    if (multi_submit())
    {
      $error = "that has already been submitted";  
    }
}

Open in new window

0
 
Ray PaseurCommented:
I want to recommend one or two things that might be helpful here.  If your time is important, you might want to hire a professional PHP developer to help you with this.  It looks like you're trying to learn PHP by reading Dreamweaver code, and unfortunately Dreamweaver contains some of the worst PHP code ever written.  (Example: the back button can cause duplicate data base updates - that's just technically incompetent design).  I'm only slightly kidding when I say that reading Dreamweaver code is like looking at a turd and wondering what went into it.

If you have a little time to step back from the issue and do some learning on your own, try hard to forget everything you have learned from Dreamweaver code.  Instead, get this excellent little book from SitePoint and work through the examples.  It will not make you a pro, but it will give you a much better foundation in PHP and MySQL.  Very readable with a code library that you can download, copy and modify for your needs.  Now in its fourth printing, it has been a permanent part of my professional library since Edition One.
http://www.sitepoint.com/books/phpmysql4/

Best of luck with your project, ~Ray
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now