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






    
        
        Sign Up
    





Note: Following code worked for me


    $['#contactForm'].submit[function [] {
       $.post["mailer.php",$["#contactForm"].serialize[], function[data]{
       }];
        return false;
    }];

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






    
        
        Sign Up
    



    $[document].ready[function[]
    {
        $['#signup'].click[function[]
    {
         $.ajax[{
          url:'index.php',
          method:'post',
          data : $['#myform'].serialize[],
          success:function[]
         {
         } 

    ]};
    ];
    }];

valdeci

12.6k6 gold badges53 silver badges78 bronze badges

answered Apr 21, 2014 at 11:34

Chetan GawaiChetan Gawai

2,3051 gold badge22 silver badges36 bronze badges

1

You can try this






    
        
        
         Sign Up
    


$[document].ready[function[]{
    $['#signup'].click[function[]{
        $.post[$[this].attr["action"], $["#myform"].serialize[],function[response]{
            alert[response] // you can get the success response return by php after submission success
        }];
    ]};
}];

answered Apr 22, 2014 at 3:47

If the form is already submitted and the user tries to refresh the webpage then the browser will attempt to resubmit the form.

If you are allowing the user to fill and submit the form for insert data. In this case, if the user refreshes the page then the data is reinserted on the MySQL database table if some action does not perform for preventing it.

In this tutorial, I show you some ways by using which you can avoid the page resubmit.

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

Chủ Đề