Solved

detect when F5 or submit

Posted on 2006-12-01
8
800 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

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

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!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

623 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