Solved

detect when F5 or submit

Posted on 2006-12-01
8
792 Views
Last Modified: 2008-01-09
hi,

Please assist how can we stop or prevent the page from adding the same record that was previously added when the user hits F5.
PHP mysql -a sample code will help.

<?php

if(@$_POST['submit']=="add")
{
$myQuery="insert into mytable (name) values( " . @$_POST['name'] . " )";
mysql_query($myQuery) or die(mysql_error());
}

<form name ="create" method="post">
<Input name="name" type="Text" <?php $_POST['name'] ?>>
<input name "submit type="submit" value="add">
</form>

Thanks.
0
Comment
Question by:FrankPorter
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 49

Assisted Solution

by:Roonaan
Roonaan earned 200 total points
ID: 18052768
You can redirect to a special thank you page:

if(@$_POST['submit']=="add")
{
  $myQuery="insert into mytable (name) values( " . mysql_real_escape_string(@$_POST['name']) . " )";
  mysql_query($myQuery) or die(mysql_error());
  header('Location:thankyou.php');
  exit();
}

-r-
0
 

Author Comment

by:FrankPorter
ID: 18052793
after redirecting to the thank you page, can we redirect again the the add page?
is there a standard way to do this rather than redirecting here and there?

Thanks.
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 18052821
You can redirect to the "add" page as well.
0
Independent Software Vendors: 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!

 

Author Comment

by:FrankPorter
ID: 18052833
Is this the standard way of dealing F5 refresh scenario ? or there's a better way of handling..

Thanks.
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 18052869
It is a solution to the F5 "the page you are trying to load was submitted using POST Data" warning.

Alternatives to duplicate prevention also account for testing if data has been recently added to database, or tracking variables in sessions.

-r-
0
 
LVL 11

Assisted Solution

by:Chris Gralike
Chris Gralike earned 100 total points
ID: 18053220
an other method might be assigning a session value to detect an F5 stroke

<?php

$show_form = false;

if(isset($_POST['submit']) ){
    session_start();
   
    if(isset($_SESSION['submitted']) ){
                $show_form = true;
                echo "Submitted!";
    }else{
                $myQuery="insert into mytable (name) values( " . mysql_real_escape_string(@$_POST['name']) . " )";
                if(mysql_query($myQuery) ){
                       $_SESSION['submitted'] = 'true';
                }else{
                      if(isset($_SESSION['count']) ){
                               $_SESSION['count'] = "1";
                      }else{
                               $_SESSION['count'] ++;
                      }
                       
                      $_SESSION['submitted'] = 'false';
                     
                      if($_SESSION['count'] < 3){
                           header('location'.$_SERVER['PHP_SELF']);
                      }else{
                           die('3 Retries Failed, please contact the administrator');
                      }
                }
    }
}else{
      if($show_form){
            print(' <table><form etc ');
      }else{
            echo "Enable Session VARS for this script to work!";
      }
}

?>


Well there are enough ways about it i guess, so good luck! and Regards,
0
 
LVL 11

Expert Comment

by:Chris Gralike
ID: 18053224
$show_form = false;         << This one should be "true"; :$

Sorry for that ;)
0
 
LVL 7

Accepted Solution

by:
Bruce Pieterse earned 200 total points
ID: 18068110
An simplier way of checking if the data exists, is by checking in the database like Roonan said earlier. You can try this:

<?php

if(@$_POST['submit']=="add") {
      $checkDB = "SELECT name FROM mytable WHERE name = '". $_POST['name'] ."'";
      $resCheckDB = mysql_db_query($checkDB);
      $recordValid = mysql_num_rows($resCheckDB);
      if($recordValid >= 1) {
            echo "Your Name is already in the Database! ";
      }
else{
      $myQuery="insert into mytable (name) values( " . @$_POST['name'] . " )";
      mysql_query($myQuery) or die(mysql_error());
      }
}

<form name ="create" method="post">
<Input name="name" type="Text" <?php $_POST['name'] ?>>
<input name "submit type="submit" value="add">
</form>
Hope that helps,
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question