jquery : submitting a form twice

前端 未结 8 980
抹茶落季
抹茶落季 2020-12-10 03:12

I\'m creating a registration form for a client for an event they\'re hosting. The basic user details are submitted to a third-party system (ie name, email etc.) but the rest

相关标签:
8条回答
  • 2020-12-10 03:22

    Here is an example code of submitting form twice to different URLs without redirection. Click on "Test" button twice.

        <!DOCTYPE html>
        <html>
        <head>
        <meta charset="ISO-8859-1">
        <title>Insert title here</title>
        <script type="text/javascript" src="jQuery.js"></script>
        <script type="text/javascript">
        $(document).ready(function() {
            $("#testBtn").click(function() {
                $("#form").submit();
                $("#form").submit(function() {
                    $("#form").attr("action", "download");
                });
            });
        });
        </script>
        </head>
        <body>
            <form id="form" enctype="multipart/form-data" method="post"
                action="upload" target="myIFrame">
                <input type="hidden" name="FileName" />
                <input type="file" name="FileDialog" size="100" />
                <input type="submit" /> <input name="txt" type="text" />
            </form>
            <input id="testBtn" type="button" value="Test" />
    
            <iframe style="display: none;" src="javascript:''" id="myIFrame">
                <html>
                    <head></head>
                    <body></body>
                </html>
            </iframe>
        </body>
        </html>
    
    0 讨论(0)
  • 2020-12-10 03:36

    Try:

    
    $('#form1').unbind('submit').submit();
    
    0 讨论(0)
  • 2020-12-10 03:36

    You can use jquery.form plugin for submitting form via ajax. Checkout following url: http://jquery.malsup.com/form/

    Then in your js script try something like this

    var myvalidator = $('#form1').bValidator(optionsGrey);
    
    $('#form1').submit(function(){
        if(myvalidator.isValid()){
          $(this).unbind('submit').submit();
    
    });
    
    0 讨论(0)
  • 2020-12-10 03:36

    I was facing the same problem. Then I noticed that I had accidentally linked my custom javascript file twice so the form was being submitted twice by two scripts. The problem was solved by removing one script link form footer of the page.

    <script src="<?php echo base_path; ?>/js/custom.js"></script>
    <script src="<?php echo base_path; ?>/js/custom.js"></script>
    
    0 讨论(0)
  • 2020-12-10 03:37

    Hm I'm not sure I understand. You are always submitting to email_send.asp so and on every success it does so, therefore it seems pretty clear why you get stuck in a loop.

    If I understand you right, the second submit should be to another url with a different success handler, right?

    So, instead of submitting the form again you could just write a basic ajax function:

    $('#form1').submit(function () {
      if (myvalidator.isValid()) {
        $.ajax({
          data: $('#form1').serialize(),
          type: "POST",
          url: "email_send.asp",
          success: function(){
            $.ajax({
              data: $('#form1').serialize(),
              type: 'POST',
              url: 'third_party.asp',
              success: function () {
                //display thank you message
              }            
            });
          }
        });
      }    
    });
    

    EDIT Here's a updated answer according to your comment:

    var myvalidator = $('#form1').bValidator(optionsGrey);
    
    $('#form1').submit(function(){      
      if(myvalidator.isValid()){
        $.ajax({
          data: $('#form1').serialize(),
          type: "POST",
          url: "email_send.asp",
          success: function(){
            $('#form1').unbind('submit').submit();
          }
        });
      }
      return false;
    });
    
    0 讨论(0)
  • 2020-12-10 03:42

    You can use unbind() to revert to the default behaviour.

    var myvalidator = $('#form1').bValidator(optionsGrey);
    
    $('#form1').submit(function() {
    
        if(myvalidator.isValid()) {
    
            $.ajax({
                data: $('#form1').serialize(),
                type: "POST",
                url: "email_send.asp",
                success: function(){
                    $('#form1').unbind('submit');
                    $('#form1').submit();
                }
            });
    
        }
    
        return false;
    
    });
    
    0 讨论(0)
提交回复
热议问题