0

I know there is at least 30+ posts for a similar 'issue' and I've been through most of them. Including this one, this one, and ones like this one (I don't want to link more because majority of them are same and I don't want to spam. Links also seemed to have broken.

index.html

  <form method="POST" name="contactform" action="contact-form.php" id="myForm"> 
    <div>
      <label for="email">Your best email address:</label>
      <input type="email" name="email" id="name" placeholder="" class="emailSubject"/>
    </div>
    <div>
     <button id="send" type="submit">SUBMIT <i class="fa fa-paper-plane"></i></button>
    </div>
  </form>

contact-form.php

<?php
$replyto =  $_POST["email"];
$sendto = 'contact@mydomain.com';
$headers = "From: Contact Form <contact@mydomain.com>";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$subject = $replyto .' is curious about more' .$replyto .'information!';
$body = "Please let me know once advancement is made on your company!";
mail($sendto, $subject ,$body, $headers);
echo 'Mail sent.'; 
?>

index.js

$('#myForm').on('submit', function(e) {
  e.preventDefault();
    $.post('contact-form.php', $(this).serialize(), function (data) {

        // This is executed when the call to mail.php was succesful.
        // 'data' contains the response from the request
    return false;
    })



      $(this).addClass('animated fadeOutRight'); $('.box form').addClass('animated fadeOut');
         $(this, '.box form').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd  oanimationend animationend', function() {
            $(this).hide();
            $('.box form').hide();
            $('.thanks').show().addClass('animated zoomIn');
            $('.box').height('auto');
         });
    });

This JS file I have modified a lot for the last 3 hours trying to figure out why preventDefault isn't triggering. Originally it was based off of the 'click' of the submit button.

By changing the order of the JS file, and triggering the animations BEFORE the AJAX, I was able to get both the transition AND the PHP to happen - However, data wasn't transferring to the PHP. So my email was coming through without the users email.

Any insight would be great - (The JS has both preventDefault() AND return false ATM as per one of the more recent suggestions.) I'm unsure how to proceed from here. Despite there being a lot of similar posts 'preventDefault' seemed to have fix all of their issues.

UPDATE

After testing with more console.log() and modifying the JS a lot I ended up copying it back to the original from THIS post.

index.html

  <form method="POST" name="contactform" action="contact-form.php" id="myForm"> 
    <div>
      <label for="email">Your best email address:</label>
      <input type="email" name="email" id="name" placeholder="" class="emailSubject"/>
    </div>
    <div>
     <button id="send" type="submit">SUBMIT <i class="fa fa-paper-plane"></i></button>
    </div>
  </form>

index.js

$('#myForm').on('submit', function(e) {
  e.preventDefault();
    $.post('contact-form.php', $(this).serialize(), function (data) {

        // This is executed when the call to mail.php was succesful.
        // 'data' contains the response from the request
    return false;
    })



      $(this).addClass('animated fadeOutRight'); $('.box form').addClass('animated fadeOut');
         $(this, '.box form').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd  oanimationend animationend', function() {
            $(this).hide();
            $('.box form').hide();
            $('.thanks').show().addClass('animated zoomIn');
            $('.box').height('auto');
         });
    });

I don't see anything different from the two samples (Which makes sense because I copied from here.) Can anyone suggest why before it was re-directing (I had numerous other people try also, same result). Possibly due to server caching, but is that normal?

1: jQuery preventDefault() not working inside AJAX call 1st SO Article

Community
  • 1
  • 1
DNorthrup
  • 827
  • 2
  • 8
  • 26
  • When are you registering the submit event? Have you made sure that at the time of the registration the element `#myForm` actually exists? (the document is ready). What happens when you simply do console log before the `e.preventDefault()` is it printing to the console? – dev7 Oct 21 '16 at 23:40
  • Besides what Yani suggested you look at, have you tried moving the `return false;` at the same level as the `e.preventDefault()` ? – Dimitris Karagiannis Oct 21 '16 at 23:43
  • Hi @Yani. To test this I ended up doing a .val() on it (Was a test I did when I was having an issue of 'no email being passes') and it was showing as data. In the current setup any console.log or debuggers are being ignored. At one point I put console.log('Hi') at every other line, but as soon as I hit submit - Right to the php page it went. – DNorthrup Oct 21 '16 at 23:45
  • @MitchKarajohn I just did at your suggestion (Didn't think of it) and didn't change anything. The only way I can get it to NOT redirect is to remove it from the form, which breaks it entirely. – DNorthrup Oct 21 '16 at 23:47
  • This doesn't make sense, what you describe sounds like it completely ignores what the `submit` event handler instructs it to do, which leads me to think what Yani was thinking: Are you **sure** that when you bind the event listener to `#myForm` the element exists in the DOM? Try running the script at the end of your body tag, after all the content of the page and let us know how that went – Dimitris Karagiannis Oct 21 '16 at 23:51
  • 1
    I would bet on 2 beers and a whiskey shot that your event is never being called, hence your form simply uses the default functionality `action="contact-form.php"`. Make sure the event is being registered correctly by using it inside a `document.ready` clause and/or put your script at the bottom of the page, after the form element surely exists. The first step is to make your event work. – dev7 Oct 22 '16 at 00:15
  • I would also like to point out that since the above code doesn't return false right away, the form is at liberty to follow through as it should. The only thing I can see that returns false is the callback inside the ajax post which does nothing for the form. – Derek Pollard Oct 22 '16 at 00:20
  • 1
    @Derek `e.preventDefault()` prevents the submission. `return false` is completely unnecessary. The return statement in the callback function is ignored. – Barmar Oct 22 '16 at 00:29
  • Put `alert("in handler")` at the beginning of the function, to be sure that it's running in the first place. – Barmar Oct 22 '16 at 00:30
  • could you comment out the code after ajax request – Beginner Oct 22 '16 at 05:41
  • Hey all - I posted an update in the original topic. The issue is resolved, but I'm wondering if anyone has insight before I close the ticket. – DNorthrup Oct 22 '16 at 16:23

0 Answers0