GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Feb-2009, 15:46
wendyz wendyz is offline
New Member
 
Join Date: Feb 2009
Posts: 6
wendyz is on a distinguished road

Refresh page without changing anything in the page


I write HTML and use 'form' to post data to next page. In next page, I $_POST[] data and store into database, it should be done for this process. But if I click "refresh button", the data will be posted from last page again, and also store the data into database, that will duplicate data, which is not what I want. so How can I do? Any good idea.
  #2  
Old 23-Feb-2009, 16:57
Blake's Avatar
Blake Blake is offline
Member
 
Join Date: Nov 2005
Posts: 255
Blake is a jewel in the roughBlake is a jewel in the rough

Re: Refresh page without changing anything in the page


I usually handle that situation by keeping a session variable, formID, which increments every time a form is submitted. Every form also has a hidden formID input. When the form is submitted, the php script that processes the form data checks if the formID value from the form matches the session variable. This works because when you refresh a form, the hidden formID that is reposted no longer matches the session variable, since the session variable would have been incremented the first time the form was submitted.

Here's a simple example. In your HTML form:

HTML Code:
<input type="hidden" name="formID" value="<?= $_SESSION['formID'] ?>">

And in the php script that processes the form:

PHP Code:

if ($_SESSION['formID'] != $_POST['formID'])
{
  echo "You refreshed your browser fool!";
}
else
{
  $_SESSION['formID']++;
  // process form
} 



You'll need to initialize the value of the session variable somewhere, probably when you initialize the session.
__________________
www.blake-foster.com
  #3  
Old 24-Feb-2009, 17:00
wendyz wendyz is offline
New Member
 
Join Date: Feb 2009
Posts: 6
wendyz is on a distinguished road

Re: Refresh page without changing anything in the page


thanks! works
  #4  
Old 24-Feb-2009, 17:08
Blake's Avatar
Blake Blake is offline
Member
 
Join Date: Nov 2005
Posts: 255
Blake is a jewel in the roughBlake is a jewel in the rough

Re: Refresh page without changing anything in the page


You're welcome.
__________________
www.blake-foster.com
  #5  
Old 29-Mar-2009, 17:30
MisterChucker's Avatar
MisterChucker MisterChucker is offline
Junior Member
 
Join Date: Mar 2009
Location: Cyberspace, Earth
Posts: 53
MisterChucker is a jewel in the roughMisterChucker is a jewel in the roughMisterChucker is a jewel in the rough

Re: Refresh page without changing anything in the page


Another solution is to use an intermediate script, as in the following example. The intermediate script is virtually invisible to the user, who can use Refresh and Back buttons without consequence.

form.php
HTML Code:
<html> <head> <title>Example Input Page</title> </head> <body> <h2>Input Page</h2> <form action="validate.php" method="post"> <input type="submit" name="btnSubmit" value="Save current time to file" /> </form> <pre>$_POST =&gt; <?php print_r($_POST); ?></pre> </body> </html>

validate.php
PHP Code:

<?php
if (isset($_POST['btnSubmit']))
{
    // Confirms that this script ran by writing the current time to a file
    file_put_contents('current_time.txt', date('Y-m-d H:i:s'));
    
    /*
     * If there are variables you want to use in confirm.php, you need to store
     * them in $_SESSION because the scope of variables is limited to
     * validate.php unless you use a require or include.
     * 
     * Remember to not echo or print anything in validate.php or you will get a
     * "headers already sent" error.
     */
    
    // Redirects to a confirmation page
    header('Location: confirm.php');
    
    /*
     * Meanwhile, the script continues. You could also put code after header().
     * 
     * One problem with this method is that no variables modified from here to
     * the end of validate.php can affect what is shown on confirm.php, even if
     * you store them in $_SESSION.
     */
}
?>


confirm.php
PHP Code:

<html>
<head>
<title>Example Confirmation Page</title>
</head>
<body>

<h2>Confirmation Page</h2>

<p>$_POST should be an empty array:</p>

<pre>$_POST =&gt; <?php print_r($_POST); ?></pre>

<p>Current time: <?php echo date('Y-m-d H:i:s'); ?></p>

<p>Try using your Refresh or Back button. Compare the current time on this page
to the time stored in current_time.txt:</p>

<pre><?php echo file_get_contents('current_time.txt'); ?></pre>

</body>
</html> 


current_time.txt
Code:
2009-03-29 16:16:21
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Misunderstanding curses refresh()? Catus C++ Forum 9 17-Jun-2008 05:50
How to add a web page I created to apache padan66 Apache Web Server Forum 2 21-Feb-2008 19:40
Loading a php page in an iframe from javascript Clive73 Web Design Forum 0 18-May-2005 07:10
Javascript: Pop Up Tutorial BobbyDouglas Web Design Forum 0 20-Nov-2003 23:19
Search Engine Positioning 101 and 201 "How To" Tips... 000 Search Engine Optimization Forum 0 29-May-2003 11:34

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:20.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.