Prevent page from submit on Refresh in PHP

This usually occurs when the user already submitted data and tries to refresh the page. In this case, the browser will attempt to POST the page again.

If you do not perform any action then a new duplicate record inserted on the MySQL Database table.

In this tutorial, I show some of the ways which you can use to avoid it.

Prevent page from submit on refresh in PHP


Contents

  1. HTML Form
  2. Redirect the user to another page
  3. Check entry in MySQL Database Table
  4. With AJAX
  5. Conclusion

 

1. HTML Form

In the demonstration, I am using the following <form> which have 4 input fields and a button.

<form method='post' action=''>
 <input type='text' placeholder='Enter username' name='txt_uname' id='txt_uname' ><br/>
 <input type='text' placeholder='Enter First name' name='txt_fname' id='txt_fname' ><br/>
 <input type='text' placeholder='Enter Last name' name='txt_lname' id='txt_lname' ><br/>
 <input type='text' placeholder='Enter email' name='txt_email' id='txt_email' ><br/>
 <input type='submit' value='Submit' id='submit' name='submit'>
</form>

 

2. Redirect the user to another page

Simply redirect the user to another page when the form successfully submitted.

Example

<?php 
include 'config.php'; // Database connection

if(isset($_POST['submit3'])){
 $uname = $_POST['txt_uname'];
 $fname = $_POST['txt_fname'];
 $lname = $_POST['txt_lname'];
 $email = $_POST['txt_email'];

 // Insert record
 $insert_query = "insert into 
                 users(username,fname,lname,email) 
                 values('".$uname."','".$fname."','".$lname."','".$email."')";
 mysql_query($insert_query);

 // Redirect to another page
 header('location: home.php');
}
?>

 

3. Check entry in MySQL Database Table

Here, you can do two things –

  • Check unique value in a field
  • Check all submit values in MySQL

Check unique value in a field

Create a field in your table which only stores unique value e.g. username, email, etc on each page submit. Check the submit value in this field if it’s not found then insert the record.

Example

<?php 
include 'config.php'; // Database connection

if(isset($_POST['submit'])){
 $uname = $_POST['txt_uname'];
 $fname = $_POST['txt_fname'];
 $lname = $_POST['txt_lname'];
 $email = $_POST['txt_email'];

 // Check username is exists or not
 $query = "select count(*) as allcount from users where username='".$uname."'";
 $result = mysql_query($query);
 $row = mysql_fetch_array($result);
 $allcount = $row['allcount'];

 // insert new record
 if($allcount == 0){
  $insert_query = "insert into 
    users(username,fname,lname,email) 
    values('".$uname."','".$fname."','".$lname."','".$email."')";
  mysql_query($insert_query);
 }
 
}
?>

Check all submit values in MySQL

You can either check all submitted values exists in MySQL database table if not then insert a new record.

Example

<?php 
include 'config.php'; // Database connection

if(isset($_POST['submit'])){
 $uname = $_POST['txt_uname'];
 $fname = $_POST['txt_fname'];
 $lname = $_POST['txt_lname'];
 $email = $_POST['txt_email'];

 // Check username is exists or not
 $query = "select count(*) as allcount from users 
          where username='".$uname."' && fname='".$fname."' && 
          lname='".$lname."' && email='".$email."'";
 $result = mysql_query($query);
 $row = mysql_fetch_array($result);
 $allcount = $row['allcount'];

 // insert new record
 if($allcount == 0){
  $insert_query = "insert into 
        users(username,fname,lname,email) 
        values('".$uname."','".$fname."','".$lname."','".$email."')";
  mysql_query($insert_query);
 }
 
}
?>

 

4. With AJAX

You can use AJAX to save data and you can either reload the page or not on successful callback.

PHP (saveData.php)

<?php 
include 'config.php'; // Database connection

$uname = $_POST['username'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];

$insert_query = "insert into 
                 users(username,fname,lname,email) 
                 values('".$uname."','".$fname."','".$lname."','".$email."')";
mysql_query($insert_query);
echo 1;
?>

jQuery

$(document).ready(function(){

 $("#submit").click(function(){
  var username = $("#txt_uname").val();
  var fname = $("#txt_fname").val();
  var lname = $("#txt_lname").val();
  var email = $("#txt_email").val();
 
  $.ajax({
   url:'saveData.php',
   type:'post',
   data:{username:username,fname:fname,lname:lname,email:email},
   success:function(response){
    location.reload(); // reloading page
   }
  });
 
 });
});

 

5. Conclusion

I showed you some of the ways by using it you can prevent the page from resubmitting. You can use any of the methods within your project.

Spread the love

Related Post

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *