Answers.Body: <p>(this is more of a jquery question than Craft, but...:))</p>
  
  <p>The reason this doesn't work is because <code>$.post</code> works asynchronously (that's the A in AJAX). It creates a request, with a success handler (the function), then the submit handler continues execution. The success handler isn't run until the request completes, which is probably a couple of hundreds of milliseconds later.</p>
  
  <p>There are various ways to solve this, but the basic idea would be to cancel the submit event straight away if the check hasn't been done, do the check, and then submit the form again if the email didn't exist, and not run the check the second time.</p>
  
  <p>Here's something a bit ugly that I think'll work, but it can be done much more elegantly, I'm sure:</p>
  
  <pre><code>var emailWasChecked = false;
  
  $("#form").on('submit', function (e) {
      if (!emailWasChecked) {
          e.preventDefault();
  
          $.post('?ok=1', {
                  action: 'formerly/submissions/alreadySubmitted',
                  formid: $("input[name=formId]").val(),
                  email: $("#formtest_email").val()
              },
              function (data, status) {
                  if (data.alreadysubmitted != true) {
                      emailWasChecked = true;
                      $("#form").trigger('submit');
                  }
              });
      } else {
          emailWasChecked = false;
      }
  });
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-31T12:00:23.023Z
Answers.Text: Awesome, thanks for the help! I figured that was the problem but did not know how to trigger the form to submit if I stopped it right away.
Answers.UserDisplayName: Nexcapto
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-30T21:25:17.523Z
Answers.LastActivityDate: 2016-03-30T21:25:17.523Z
Answers.UserDisplayName: André Elvan
Body: <p>So, I have a form created using formerly and their <a href="https://github.com/xodigital/Formerly/blob/master/form.html" rel="nofollow">template</a>.</p>
  
  <p>The form works fine, but I am trying to check for duplicate e-mails and just stop a submission when it notices there is one in the system. The following works and the console spits out true on data.alreadysubmitted but the line below does not stop the post. How should I handle this?</p>
  
  <p>(I know the code needs cleanup. It will be done after functionality is met. I still need to check date of submission after finding an email duplicate to enable a 1 entry per day style form)</p>
  
  <pre><code>$("#form").on('submit', function (e) {
          console.log($("#formtest_email").val());
          $.post('?ok=1', {
              action: 'formerly/submissions/alreadySubmitted',
              formid: $("input[name=formId]").val(),
              email: $("#formtest_email").val()
          },
          function(data, status){
              console.log("already?:" + data.alreadysubmitted + "\nStatus: " + data.ok);
              if (data.alreadysubmitted == true) 
              {
                   e.preventDefault();
              }
          });
  });
  </code></pre>
  
  <p><strong>Update:</strong> After trying to store data.alreadysubmitted outside of the function, I realize that the function executes after anything below it (outside of the whole .post method). Would it be right to assume that the function is called after the submit is complete, and hence e.preventDefault will not work?</p>
  
Tags: <ajax><plugins-formerly>
Title: Formerly - Checking if e-mail already entered
ThreadText: # Formerly - Checking if e-mail already entered
  
  (Asked by: username_0 on 2016-03-30)
  
  So, I have a form created using formerly and their [template](https://github.com/xodigital/Formerly/blob/master/form.html).
  
  The form works fine, but I am trying to check for duplicate e-mails and just stop a submission when it notices there is one in the system. The following works and the console spits out true on data.alreadysubmitted but the line below does not stop the post. How should I handle this?
  
  (I know the code needs cleanup. It will be done after functionality is met. I still need to check date of submission after finding an email duplicate to enable a 1 entry per day style form)
  
  ```
  $("#form").on('submit', function (e) {
          console.log($("#formtest_email").val());
          $.post('?ok=1', {
              action: 'formerly/submissions/alreadySubmitted',
              formid: $("input[name=formId]").val(),
              email: $("#formtest_email").val()
          },
          function(data, status){
              console.log("already?:" + data.alreadysubmitted + "\nStatus: " + data.ok);
              if (data.alreadysubmitted == true) 
              {
                   e.preventDefault();
              }
          });
  });
  
  ```
  
  **Update:** After trying to store data.alreadysubmitted outside of the function, I realize that the function executes after anything below it (outside of the whole .post method). Would it be right to assume that the function is called after the submit is complete, and hence e.preventDefault will not work?
  
  **Tags:** ajax, plugins-formerly
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-03-30. Score: 2 | Accepted answer
  
  (this is more of a jquery question than Craft, but...:))
  
  The reason this doesn't work is because `$.post` works asynchronously (that's the A in AJAX). It creates a request, with a success handler (the function), then the submit handler continues execution. The success handler isn't run until the request completes, which is probably a couple of hundreds of milliseconds later.
  
  There are various ways to solve this, but the basic idea would be to cancel the submit event straight away if the check hasn't been done, do the check, and then submit the form again if the email didn't exist, and not run the check the second time.
  
  Here's something a bit ugly that I think'll work, but it can be done much more elegantly, I'm sure:
  
  ```
  var emailWasChecked = false;
  
  $("#form").on('submit', function (e) {
      if (!emailWasChecked) {
          e.preventDefault();
  
          $.post('?ok=1', {
                  action: 'formerly/submissions/alreadySubmitted',
                  formid: $("input[name=formId]").val(),
                  email: $("#formtest_email").val()
              },
              function (data, status) {
                  if (data.alreadysubmitted != true) {
                      emailWasChecked = true;
                      $("#form").trigger('submit');
                  }
              });
      } else {
          emailWasChecked = false;
      }
  });
  
  ```
  
  > **Comments:**
  > **username_0**: Awesome, thanks for the help! I figured that was the problem but did not know how to trigger the form to submit if I stopped it right away.  
  
  ---
  
  