How to use JavaScript to prevent form submission when the page is reloaded

edited May 2023 in Web Development

Example:

Upon submission of the form, a popup appears. But when the page is reloaded, the form is not submitted.

<script>

if ( window.history.replaceState ) {

      window.history.replaceState( null, null, window.location.href );

    }

</script>


  • During real-time scenarios using core PHP:

if ($result) {

echo '<script type="text/javascript">';

    echo 'setTimeout(function () { swal("Success  ! ","Successfully Transfered  ","success");';

    echo '}, 2000); if ( window.history.replaceState ) {

      window.history.replaceState( null, null, window.location.href );

    }</script>';

  } else {

    echo '<script type="text/javascript">';

    echo 'setTimeout(function () { swal("Error  ! ","An Error Occured, Please Try Again Later","error");';

    echo '}, 2000); if ( window.history.replaceState ) {

      window.history.replaceState( null, null, window.location.href );

    }</script>';

  }

Sign In or Register to comment.