skip to Main Content

Can someone help a JS newbie?

Almost everything is working, results are returned, nothing opens new tabs, forms submit to MC database….however I cannot get the result html to post to the correct DIV. All results are being posted to the footer div.

I am guessing my selectors are not specific enough? But I do not have the knowledge on how to structure correctly.

2 forms on page using AJAX submit.
1 pop up form and 1 form in footer….. but all the result html is posting the the div in the footer.

I have adjusted the function register names as suggested (and updated the code below), but form result data is still going to the footer div

//JAVASCRIPT


// FOOTER FORM. waits for form to appear rather than appending straight to the form. Also helps if you have more than one type of form that you want to use this action on. 
$(document).on('submit', '#footer-mc-embedded-subscribe-form', function(event) {
    try {
      //define argument as the current form especially if you have more than one
      var $registerFooterFormbutton= jQuery(this);
      // stop open of new tab
      event.preventDefault();
      // submit form via ajax
      register($registerFooterFormbutton);
    } catch(error){}
  });

// POP UP FORM. waits for form to appear rather than appending straight to the form. Also helps if you have more than one type of form that you want to use this action on. 
$(document).on('submit', '#pop-mc-embedded-subscribe-form', function(event) {
    try {
      //define argument as the current form especially if you have more than one
      var $registerPopUpFormbutton= jQuery(this);
      // stop open of new tab
      event.preventDefault();
      // submit form via ajax
      register($registerPopUpFormbutton);
    } catch(error){}
  });

// POP UP FORM. post result to div
function register($registerPopUpForm) {
  $('#pop-mc-embedded-subscribe-form').val('Sending...');
  $.ajax({
    type: 'GET',
    url: 'https://websitename.us16.list-manage.com/subscribe/post-json?u=.....&c=?',
    data: $registerPopUpForm.serialize(),
    cache: false,
    dataType: 'jsonp',
    contentType: 'application/json; charset=utf-8',
    error: function (err) { alert('Could not connect to the registration server. Please try again later.') },
    success: function (data) {
      $('#pop-mc-embedded-subscribe-form').val('pop-subscribe')
      if (data.result === 'success') {
        // Yeahhhh Success
    console.log(data.msg)
        $('#pop-mce-EMAIL').css('borderColor', '#ffffff')
        $('#pop-subscribe-result').css('color', 'rgb(53, 114, 210)')
         $("#pop-subscribe-result").html(data['msg']); 
        $('#pop-mce-EMAIL').val('')
      } else {
        // Something went wrong, do something to notify the user.
     console.log(data.msg)
        $('#pop-mce-EMAIL').css('borderColor', '#ff8282')
        $('#pop-subscribe-result').css('color', '#ff8282')
        $("#pop-subscribe-result").html(data['msg']);  
      }
    }
  })
};


// FOOTER FORM. post result to div
function register($registerFooterForm) {
  $('#footer-mc-embedded-subscribe-form').val('Sending...');
  $.ajax({
    type: 'GET',
    url: 'https://websitename.us16.list-manage.com/subscribe/post-json?u=.....&c=?',
    data: $registerFooterForm.serialize(),
    cache: false,
    dataType: 'jsonp',
    contentType: 'application/json; charset=utf-8',
    error: function (err) { alert('Could not connect to the registration server. Please try again later.') },
    success: function (data) {
      $('#footer-mc-embedded-subscribe-form').val('footer.subscribe')
      if (data.result === 'success') {
        // Yeahhhh Success 
    console.log(data.msg)
        $('#footer-mce-EMAIL').css('borderColor', '#ffffff')
        $('#footer-subscribe-result').css('color', 'rgb(53, 114, 210)')
         $("#footer-subscribe-result").html(data['msg']); 
        $('#footer-mce-EMAIL').val('')
      } else {
        // Something went wrong, do something to notify the user.
     console.log(data.msg)
        $('#footer-mce-EMAIL').css('borderColor', '#ff8282')
        $('#footer-subscribe-result').css('color', '#ff8282')
        $("#footer-subscribe-result").html(data['msg']); 
      }
    }
  })
};
<!--HTML POP UP FORM-->

<form
  action="mailchimp url"
  method="post"
  name="pop-form"
  id="pop-mc-embedded-subscribe-form"
  class=""
  target="_blank"
  novalidate
>
  <div class="form-group">
    <input
      type="email"
      name="EMAIL"
      class="form-control required"
      placeholder="Enter your e-mail"
      id="pop-mce-EMAIL"
    />
    <input
      type="submit"
      value="SUBSCRIBE HERE"
      name="pop-subscribe"
      id="pop-mc-embedded-subscribe"
      class="button"
    />
  </div>
  <div id="pop-subscribe-result"></div>
</form>

<!--FOOTER FORM HTML-->

<form
  action="mailchimp url"
  method="post"
  id="footer-mc-embedded-subscribe-form"
  name="footer-form"
  class=""
  target="_blank"
  novalidate
>
  <div class="mc-field-group">
    <label for="mce-EMAIL"
      >Email Address <span class="asterisk">*</span>
    </label>
    <input
      type="email"
      value=""
      name="EMAIL"
      class="form-control required email"
      id="footer-mce-EMAIL"
      placeholder="Email Address *"
    />
  </div>
  <div class="mc-field-group">
    <label for="mce-FNAME">First Name </label>
    <input
      type="text"
      value=""
      name="FNAME"
      class="form-control"
      id="mce-FNAME"
      placeholder="First Name"
    />
  </div>
  <div class="mc-field-group">
    <label for="mce-LNAME">Last Name </label>
    <input
      type="text"
      value=""
      name="LNAME"
      class="form-control"
      id="mce-LNAME"
      placeholder="Last Name"
    />
  </div>

  <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
  <div style="position: absolute; left: -5000px;" aria-hidden="true">
    <input
      type="text"
      name="b_dc51fb25cd808abedc98e3ff2_ea4d259202"
      tabindex="-1"
      value=""
    />
  </div>
  <div class="footer-btn">
    <input
      type="submit"
      value="Subscribe"
      name="footer-subscribe"
      id="mc-embedded-subscribe"
      class="button"
    />
  </div>
  <div id="footer-subscribe-result"></div>
</form>

2

Answers


  1. You are defining the register() function two times with the same name. The second one overwrites the first and everytime you call the function with that name you call the second function. An easy solution is to change the name of the functions (i.e registerPopUpForm() , registerFooterForm() ) and use them accordingly.

    Login or Signup to reply.
  2. You have two functions with the same name "register" so when you press the submit button in either forms it runs in the register function in the footer since it has the same name as the one dedicated to the popup form

    Use this code and your form will work as expected:

    //JAVASCRIPT
    
    
    // FOOTER FORM. waits for form to appear rather than appending straight to the form. Also helps if you have more than one type of form that you want to use this action on. 
    $(document).on('submit', '#footer-mc-embedded-subscribe-form', function(event) {
        try {
          //define argument as the current form especially if you have more than one
          var $registerFooterFormbutton= jQuery(this);
          // stop open of new tab
          event.preventDefault();
          // submit form via ajax
          register1($registerFooterFormbutton);
        } catch(error){}
      });
    
    // POP UP FORM. waits for form to appear rather than appending straight to the form. Also helps if you have more than one type of form that you want to use this action on. 
    $(document).on('submit', '#pop-mc-embedded-subscribe-form', function(event) {
        try {
          //define argument as the current form especially if you have more than one
          var $registerPopUpFormbutton= jQuery(this);
          // stop open of new tab
          event.preventDefault();
          // submit form via ajax
          register($registerPopUpFormbutton);
        } catch(error){}
      });
    
    // POP UP FORM. post result to div
    function register($registerPopUpForm) {
      $('#pop-mc-embedded-subscribe-form').val('Sending...');
      $.ajax({
        type: 'GET',
        url: 'https://websitename.us16.list-manage.com/subscribe/post-json?u=.....&c=?',
        data: $registerPopUpForm.serialize(),
        cache: false,
        dataType: 'jsonp',
        contentType: 'application/json; charset=utf-8',
        error: function (err) { alert('Could not connect to the registration server. Please try again later.') },
        success: function (data) {
          $('#pop-mc-embedded-subscribe-form').val('pop-subscribe')
          if (data.result === 'success') {
            // Yeahhhh Success
        console.log(data.msg)
            $('#pop-mce-EMAIL').css('borderColor', '#ffffff')
            $('#pop-subscribe-result').css('color', 'rgb(53, 114, 210)')
             $("#pop-subscribe-result").html(data['msg']); 
            $('#pop-mce-EMAIL').val('')
          } else {
            // Something went wrong, do something to notify the user.
         console.log(data.msg)
            $('#pop-mce-EMAIL').css('borderColor', '#ff8282')
            $('#pop-subscribe-result').css('color', '#ff8282')
            $("#pop-subscribe-result").html(data['msg']);  
          }
        }
      })
    };
    
    
    // FOOTER FORM. post result to div
    function register1($registerFooterForm) {
      $('#footer-mc-embedded-subscribe-form').val('Sending...');
      $.ajax({
        type: 'GET',
        url: 'https://websitename.us16.list-manage.com/subscribe/post-json?u=.....&c=?',
        data: $registerFooterForm.serialize(),
        cache: false,
        dataType: 'jsonp',
        contentType: 'application/json; charset=utf-8',
        error: function (err) { alert('Could not connect to the registration server. Please try again later.') },
        success: function (data) {
          $('#footer-mc-embedded-subscribe-form').val('footer.subscribe')
          if (data.result === 'success') {
            // Yeahhhh Success 
        console.log(data.msg)
            $('#footer-mce-EMAIL').css('borderColor', '#ffffff')
            $('#footer-subscribe-result').css('color', 'rgb(53, 114, 210)')
             $("#footer-subscribe-result").html(data['msg']); 
            $('#footer-mce-EMAIL').val('')
          } else {
            // Something went wrong, do something to notify the user.
         console.log(data.msg)
            $('#footer-mce-EMAIL').css('borderColor', '#ff8282')
            $('#footer-subscribe-result').css('color', '#ff8282')
            $("#footer-subscribe-result").html(data['msg']); 
          }
        }
      })
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search