0

I have the following form:

<form action="myaction" method="post" enctype="multipart/form-data">
      <span class="required">*</span>Email</td>
      <input type="text" name="email" id="moosendid" />
      <input type="submit" id="submit1" value="submit" class="button" />
    </form>

and a javascript function:

function MoosendMail(moomail) {...}

I need on submit to run the function before the action of the form. I tried something like this:

$(document).ready(function() {
  $('input#submit1').on('click',function() {
    MoosendMail($('input#moosendid').val());
  });
});

with no success.

Any ideas?

4
  • Why not add the callback on submit of the form? You've got it on the click of the button which probably fires after the submit. Commented Mar 24, 2017 at 14:41
  • with no success. - meaning what? What happens? Nothing? Something? Errors? Commented Mar 24, 2017 at 14:42
  • The function i use is from moosend and i am trying to get user email to put it in a mailing list. I have tested function alone and its working, but this form is registration form with a checkbox for newsletter registration, so i wanted if clicked yes to register user and execute function to put user in mailing list Commented Mar 24, 2017 at 14:45
  • 2
    You added method type post on your form. So you need to first stop submitting form by e.preventDefault() Commented Mar 24, 2017 at 14:55

2 Answers 2

1

You'll need to use the submit handler and then prevent the default action of the submit button I believe.

function MoosendMail(moomail){
    alert(moomail);
};

  $(document).ready(function() {  
    $('#form').submit(function(e){
        e.preventDefault();     
        MoosendMail($('input#moosendid').val());
        this.submit(); // only after you are done (be careful of async requests) and want to submit.
    });
  });

I hope this help.

Sign up to request clarification or add additional context in comments.

2 Comments

It seems that when i console.log the email value from input before submit, i get nothing, like i don;t get the value. Any ideas?
I'm not sure. The alert shows my file. Can you update your question with code you are working with?
1

You added method type post on your form. So you need to first stop submitting form by e.preventDefault(). Try this:

$(document).ready(function() {
  $('input#submit1').on('click',function(e) {
     e.preventDefault();
 
    MoosendMail($('input#moosendid').val());
  });
});

function MoosendMail(moomail) {
   console.log(moomail);
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="myaction" method="post" enctype="multipart/form-data">
  <span class="required">*</span>Email</td>
  <input type="text" name="email" id="moosendid" />
  <input type="submit" id="submit1" value="submit" class="button" />
</form>

2 Comments

In this example after MoosendMail function i wrote this:console.log(" MoosendMail("+$('input#moosendid').val()+")"); and on submit i got in console MoosendMail(). It seems like i don't get the value
You added as string so you are getting string. If you run my code here and submit form you see it's working. or you need to type console.log(MoosendMail($('input#moosendid').val()));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.