title: Javascript form with jQuery submit running submit twice
body: <p>I am adding some functionality to an existing Drupal 7 site and have had to throw away a working webforms form as it is not possible to add some required functionality. I have replaced the in built webform with a custom module that generates the page and presents a profile update form to the user. When I submit the form it seems the form is submitting twice and the first time around there appears to be an error in the Firefox JS console. I have done a lot of reading and testing and I do not understand what is going on here.</p>
  
  <p>This form can be viewed at the following location</p>
  
  <p><a href="http://www.tztesting.com/userprofile" rel="nofollow">http://www.tztesting.com/userprofile</a></p>
  
  <p>To attempt to use this form you must log in to the site as the form submit makes calls to a number of API's that require authentication. The test username is web@tz.net and the test password is abcd1234</p>
  
  <p>The html and JS for the form is as follows</p>
  
  <pre><code>&lt;div class="panel-pane pane-block profile-top profile-edit"&gt;
  
    &lt;div class="pane-content"&gt;
      &lt;div class="update_message"&gt;&lt;/div&gt;
      &lt;form accept-charset="UTF-8" id="profile_edit" method="post" action="" enctype="multipart/form-data" class="webform-client-form "&gt;&lt;div&gt;&lt;div class="form-item webform-component webform-component-markup webform-component--form-title"&gt;
            &lt;p&gt;Change Account Details&lt;/p&gt;
          &lt;/div&gt;
          &lt;div class="form-item webform-component webform-component-textfield webform-component--first-name webform-container-inline"&gt;
            &lt;label for="edit-submitted-first-name"&gt;First name &lt;span title="This field is required." class="form-required"&gt;*&lt;/span&gt;&lt;/label&gt;
            &lt;input type="text" maxlength="128" size="60" value="Test" name="submitted[first_name]" id="edit-submitted-first-name" class="form-control form-text firstname"&gt;
          &lt;/div&gt;
          &lt;div class="form-item webform-component webform-component-textfield webform-component--last-name webform-container-inline"&gt;
            &lt;label for="edit-submitted-last-name"&gt;Last name &lt;span title="This field is required." class="form-required"&gt;*&lt;/span&gt;&lt;/label&gt;
            &lt;input type="text" maxlength="128" size="60" value="User" name="submitted[last_name]" id="edit-submitted-last-name" class="form-control form-text lastname"&gt;
          &lt;/div&gt;
          &lt;div class="form-item webform-component webform-component-textfield webform-component--mobile-no webform-container-inline"&gt;
            &lt;label for="edit-submitted-mobile-no"&gt;Mobile no. &lt;span title="This field is required." class="form-required"&gt;*&lt;/span&gt;&lt;/label&gt;
            &lt;input type="text" maxlength="128" size="64" value="85112366" name="submitted[mobile_no]" id="edit-submitted-mobile-no" class="form-control form-text number"&gt;
          &lt;/div&gt;
          &lt;div class="form-item webform-component webform-component-textfield webform-component--postal-code webform-container-inline"&gt;
            &lt;label for="edit-submitted-postal-code"&gt;Postal Code &lt;span title="This field is required." class="form-required"&gt;*&lt;/span&gt;&lt;/label&gt;
            &lt;input type="text" maxlength="128" size="64" value="408600" name="submitted[postal_code]" id="edit-submitted-postal-code" class="form-control form-text postcode" onkeyup="getaddress();"&gt;
          &lt;/div&gt;
          &lt;div class="form-item webform-component webform-component-textfield webform-component--building-name webform-container-inline"&gt;
            &lt;label for="edit-submitted-building-name"&gt;Building name &lt;/label&gt;
            &lt;input type="text" maxlength="128" size="60" value="SINGAPORE POST CENTRE" name="submitted[building_name]" id="edit-submitted-building-name" class="form-control form-text building_name"&gt;
          &lt;/div&gt;
          &lt;div class="form-item webform-component webform-component-textfield webform-component--street-name webform-container-inline"&gt;
            &lt;label for="edit-submitted-street-name"&gt;Street name &lt;/label&gt;
            &lt;input type="text" maxlength="128" size="60" value="10 EUNOS ROAD 8" name="submitted[street_name]" id="edit-submitted-street-name" class="form-control form-text street_name"&gt;
          &lt;/div&gt;
          &lt;div class="form-item webform-component webform-component-textfield webform-component--unit-number webform-container-inline"&gt;
            &lt;label for="edit-submitted-unit-number"&gt;Unit number &lt;/label&gt;
            &lt;input type="text" maxlength="128" size="64" value="122" name="submitted[unit_number]" id="edit-submitted-unit-number" class="form-control form-text unit_number"&gt;
          &lt;/div&gt;
          &lt;div class="form-item webform-component webform-component-checkboxes webform-component--tc-acknowlegement"&gt;
            &lt;label for="edit-submitted-tc-acknowlegement" class="element-invisible"&gt;T&amp;amp;C acknowlegement &lt;span title="This field is required." class="form-required"&gt;*&lt;/span&gt;&lt;/label&gt;
            &lt;div class="form-checkboxes" id="edit-submitted-tc-acknowlegement"&gt;&lt;div class="form-type-checkbox form-item-submitted-tc-acknowlegement-y form-item checkbox"&gt;
                &lt;label for="edit-submitted-tc-acknowlegement-1"&gt;&lt;input type="checkbox" class="form-checkbox" value="acknowlegement" name="submitted[tc_acknowlegement]" id="edit-submitted-tc-acknowlegement-1"&gt; I acknowledge and accept the &lt;a href="/privacy_policy" target="_blank"&gt;Privacy Policy&lt;/a&gt;, &lt;a href="/terms_of_use" target="_blank"&gt;Website Terms of Use&lt;/a&gt; and &lt;a href="/popstation-terms-conditions" target="_blank"&gt;POPStation T&amp;amp;C&lt;/a&gt; &lt;/label&gt;
              &lt;/div&gt;
            &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-item webform-component webform-component-checkboxes webform-component--information-acknowledgement"&gt;
            &lt;label for="edit-submitted-information-acknowledgement" class="element-invisible"&gt;Information acknowledgement &lt;/label&gt;
            &lt;div class="form-checkboxes" id="edit-submitted-information-acknowledgement"&gt;&lt;div class="form-type-checkbox form-item-submitted-information-acknowledgement-y form-item checkbox"&gt;
                &lt;label for="edit-submitted-information-acknowledgement-1"&gt;&lt;input type="checkbox" class="form-checkbox" value="y" name="submitted[information_acknowledgement][y]" id="edit-submitted-information-acknowledgement-1"&gt; I consent to the collection, use and disclosure of my personal data by the SingPost Group for the purposes of marketing products or services offered by the SingPost Group. &lt;/label&gt;
              &lt;/div&gt;
            &lt;/div&gt;
          &lt;/div&gt;
          &lt;button type="submit" value="Confirm" name="op" class="webform-submit button-primary btn btn-primary form-submit"&gt;Confirm&lt;/button&gt;
        &lt;/div&gt;
      &lt;/form&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;script type="text/javascript"&gt;
  
    (jQuery)(".form-submit").click(function(){
  
        var arr = {
          "FirstName": jQuery(".firstname").val(),
          "LastName": jQuery(".lastname").val(),
          "ContactNumber": jQuery(".number").val(),
          "PostCode": jQuery(".postcode").val(),
          "BuildingName": jQuery(".building_name").val(),
          "StreetName": jQuery(".street_name").val(),
          "UnitNumber": jQuery(".unit_number").val()
        };
  
        var out_arr = JSON.stringify(arr);
  
        var update = (jQuery).ajax({
          url: "/userprofile?update=" + out_arr,
          method: "POST",
          async: "true"
        });
  
        update.done(function (json) {
          objects_update = JSON.parse(json);
          if (objects_update.Success) {
            var html = "&lt;h3&gt;Your profile has been updated&lt;/h3&gt;";
            (jQuery)(".update_message").html(html);
          }
        });
        /**************/
        alert("done");
        /*************/
      });
  
  
    function getaddress() {
      var postcode = jQuery(".postcode").val();
  
      if (postcode.length == 6) {
  
        var address = (jQuery).ajax({
          url: "/userprofile?edit=" + postcode,
          method: "POST",
          async: "true"
        });
  
        address.done(function( json ) {
          objects_address = JSON.parse(json);
          if (objects_address.Success) {
            var code = objects_address.Address.PostalCode;
            var street_name = objects_address.Address.StreetName;
            var building_name = objects_address.Address.BuildingName;
            var building_no = objects_address.Address.BuildingNo;
  
            jQuery( ".building_name" ).val( building_name );
            jQuery( ".street_name" ).val( building_no + " " + street_name );
          }
        });
      }
    }
  
  &lt;/script&gt;
  </code></pre>
  
  <p>In the Javascript there is an alert("done"); and without this the submit does not work. Of course this alert is not wanted.</p>
  
  <p>If you look at the console when the form is submitted it appears that the form is submitting twice and the first time around it hits the alert("done") while the second the submit appears to happen before the alert("done");</p>
  
  <p>My desired functionality is that When the /userprofile URL is hit the Drupal php module pulls the current data from the API and pushes it into this form as default values. When the user submits this form the PHP module receives the values, as a serialized array, and processes them and sends the new values to the API. After the submit a JSON string is returned that looks like this </p>
  
  <pre><code>{"Success":true,"Data":{}}
  </code></pre>
  
  <p>Once parsed all we check for is the Success value being true and if it is a message is made available in the "update message" div at the top of the form.</p>
  
  <p>I would appreciate it if someone could look at this form and let me know what I am doing wrong as I have been tearing my hair out for some time trying to sort this out.</p>
  
  <p>Regards</p>
  
  <p>Richard</p>