How to avoid page reload in php form submission

后端 未结 3 1296
心在旅途
心在旅途 2020-12-06 15:54

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 th

相关标签:
3条回答
  • You can try this

    <html>
    <head>
    </head>
    
    <body>
        <form id="myform" action="index.php"><!--changge-->
            <input type="email" placeholder="Enter email"  name="email_address">
            <button class="btn btn-primary custom-button red-btn" id="signup">
             Sign Up</button>
        </form>
    
    <script>
    $(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
            });
        )};
    });
    

    0 讨论(0)
  • 2020-12-06 16:08

    Using ajax

    HTML

    <html>
    <head>
    </head>
    
    <body>
        <form id="myform"><!--changge-->
            <input type="email" placeholder="Enter email"  name="email_address">
            <button class="btn btn-primary custom-button red-btn" id="signup">Sign Up</button>
        </form>
    </body>
    </html>
    
    <script>
        $(document).ready(function()
        {
            $('#signup').click(function()
        {
             $.ajax({
              url:'index.php',
              method:'post',
              data : $('#myform').serialize(),
              success:function()
             {
             } 
    
        )};
        );
        });
    </script>
    
    0 讨论(0)
  • 2020-12-06 16:14

    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

    <form action="index.php" method="post" role="form" target="_blank">
    
    0 讨论(0)
提交回复
热议问题