1

Today I learned some jQuery.

<script>    
$('#mc-embedded-subscribe-form').on('submit', function(e){
e.preventDefault();
email=document.mc-embedded-subscribe-form.EMAIL.value;
$.post( "email.php", {email});
$('#fadeout').fadeOut(500);
$('#fadein').delay(500).fadeIn(500);
});
</script>

This is a script I'm running upon the event of a user submitting a form. The problem I'm having is that as soon as the form is submitted I get redirected to email.php, whereas I want to stay on the initial webpage. If I remove the line

$.post( "email.php", {email});

The rest works as expected. email.php works fine, it simply saves the email. What can I do to stop the redirect?

Bas Slagter
  • 9,831
  • 7
  • 47
  • 78
ShakesBeer
  • 283
  • 4
  • 15

2 Answers2

1

Since there's an error in your $.post() line the e.preventDefault() is not kicking in. Edit your code to the following:

$('#mc-embedded-subscribe-form').on('submit', function(e){
    e.preventDefault();
    email = $('[name=EMAIL]',this).val();
    $.post( "email.php", {email:email}, function() {
        $('#fadeout').fadeOut(500);
        $('#fadein').delay(500).fadeIn(500);
    });
});
PeterKA
  • 24,158
  • 5
  • 26
  • 48
1

if you want to post specific value from client side to server than its better to use $.ajax method then $.post i.e:

<script>    

    $('#mc-embedded-subscribe-form').on('submit', function(e){
    e.preventDefault();
    email=document.mc-embedded-subscribe-form.EMAIL.value;
    $.ajax({

        url:"email.php";
        data: email;
        success:function(){
           $('#fadeout').fadeOut(500);
           $('#fadein').delay(500).fadeIn(500);
        }

    });

});
</script>

$.ajax post specific value or values to server side without redirection of the page.