Jquery form submission is not working

2019-08-13 16:21发布

问题:

I try to submit a form after my ajax request done. I do code like following. But when I click the button ajax request is go to and send 200 ok feedback. But after that form not submit. What is wrong on this code?

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
         <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
         <script>

       $('document').ready(function(){
           $('#submit').on('click',function(){
        $.post('cart.php',{email:'email',pkgname:'pkgname'}).then(function(){
           $('form').submit();
        });
    });
       });


         </script>
    </head>
    <body>

        <form action="cart.php" method="post">
            <input type="button" id="submit" value="submit" />
        </form>


    </body>
</html>

回答1:

try to build the $.post request like:

$.post('cart.php', {email:'email',pkgname:'pkgname'}, funtion(data){
   console.log(data);//return the data from server if anything echoed in cart.php file
})

where data contains what cart.php returns (i cannot understand why you are sending the form twice anyway)



回答2:

I update code as below and I think it is not getting response from sever I think u update this and check you will get your problem :

 $('document').ready(function(){
           $('#submit').on('click',function(){
        $.post('cart.php',{email:'email',pkgname:'pkgname'}).success(function(data){
           $('form').submit();
        }).error(alert("Code has error"));
    });
       });



回答3:

Here answer for my question. I don't know how is this possible??? :( My submit button id is the cause for my question. You never ever use #submit as a ID. It will be a problem!! This bugger wast my two days!!

So correct code is

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
         <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
         <script>

       $('document').ready(function(){
           $('#submit1').on('click',function(){
        $.post('cart.php',{email:'email',pkgname:'pkgname'}).then(function(){
           $('form').submit();
        });
    });
       });


         </script>
    </head>
    <body>

        <form action="cart.php" method="post">
            <input type="button" id="submit1" value="submit" />
        </form>


    </body>
</html>