How to stop page refresh on form submit in php

I am currently using php in my html page and there is an action in the same page which gets executed upon form submission. Currently whole form gets reloaded while I want the php action to be run in the background with out reloading. How do I do it. I would also want to clear the text box after submit button is clicked. Following is the code I am currently using






    

Note: Following code worked for me


asked Apr 21, 2014 at 11:24

rajuraju

4,64414 gold badges62 silver badges116 bronze badges

1

You need to use AJAX for this, for without reloading. Or, if you want to do it without disturbing the page, you need to set target.

Using AJAX

$("form").submit(function(){
  $.post($(this).attr("action"), $(this).serialize());
  return false;
});

Using target

answered Apr 21, 2014 at 11:26

2

Using ajax

HTML






    
        
        
    



How to stop page refresh on form submit in php

valdeci

12.6k6 gold badges53 silver badges78 bronze badges

answered Apr 21, 2014 at 11:34

How to stop page refresh on form submit in php

Chetan GawaiChetan Gawai

2,3051 gold badge22 silver badges36 bronze badges

1

You can try this






    

How to stop page refresh on form submit 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 example, I am using the following

that has 4 input fields and a button.


  




2. Redirect the user to another page

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

Example


3. Check entry in MySQL Database Table

Here, you can do two things –

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

Check the 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



Check all submit values in MySQL

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

Example


4. With AJAX

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

PHP (saveData.php)

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 on your webpage.

If you found this tutorial helpful then don't forget to share.

How do I stop form submit from refresh in PHP?

After inserting it to database, call unset() method to clear the data. unset($_POST); To prevent refresh data insertion, do a page redirection to same page or different page after record insert.

How do I stop form submit on page refresh?

Use the preventDefault() method on the event object to prevent a page refresh on form submit in React, e.g. event. preventDefault() . The preventDefault method prevents the browser from issuing the default action which in the case of a form submission is to refresh the page.

How do I stop page refresh on form submit laravel?

html prevent submit from reloading page.
.
.
.

How do I stay on the same page after submitting form in PHP?

There are two ways of doing it:.
Submit the form to the same page: Handle the submitted form using PHP script. (This can be done by setting the form action to the current page URL.) ... .
Using AJAX Form Submission which is a little more difficult for a beginner than method #1..