Forum Moderators: coopster

Message Too Old, No Replies

FIX:: $_POST Refresh Problem

         

Holzberg

1:31 pm on Jun 8, 2005 (gmt 0)

10+ Year Member



This is in response to: [webmasterworld.com...] which is now closed.

Here is an easy way to get around the refreshing of a page with post data and records (or whatever is being done) get created multiple times.

On the page with the form make it a php page with session_start(); on the very top. Set a session variable such as:

$_SESSION['newform'] = 1;

Now, when they submit the form and go to the next page (this will need to be a php page with session_start(); at the top) check to see if the variable we created is set. If it is, then do what you need to with the form data and unset the session variable. If it is not set, then say the data has already been entered.

Here is an example:
--- BEGIN CODE ----
<?php
session_start();

if(isset($_SESSION['newform']){
echo "adding data to database"; //(Replace this echo with the commands to do what you want with the data.
unset($_SESSION['newform']);
} else {
echo "Data already added to database";
}

?>
--- END CODE ---

Another thing you could do is check to see if the session is set to 1...if it is, then add the data and set it to 2. Then you know that if it is 2, the data has been sent, if not 1 or 2, then they never visited the form page.

Here is an example of this:
--- BEGIN CODE ---
<?php
session_start();

if($_SESSION['newform'] == 1){
echo "Data being entered into database";//Replace with actual code to do what you wish with the data.
$_SESSION['newform'] = 2;
} else if ($_SESSION['newform'] == 2){
echo "Data already added";
} else {
echo "Please go <a href='myform.php'>HERE</a> to submit your data"; //or simply forward them to the page.
}
?>
--- END CODE ---

This has worked great for me. Enjoy :)

coopster

4:34 pm on Jun 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



And yet another way to get things done in PHP ;)

Welcome to WebmasterWorld, Holzberg.

Holzberg

3:24 am on Jun 9, 2005 (gmt 0)

10+ Year Member



Thanks.

I love helping people :)