jagmitg
jagmitg

Reputation: 4546

Ajax not firing on submission

I am trying to create a form that has an action to a URL. But it requires ajax submission first and then it should submit to the action.

It should send me an email (within the newsletter.php).

<script type='text/javascript'>
$(function () {
    $("#signup").on('submit', function( e, response ) {
        if (response.errors == false) {
            $.ajax({
                type : 'GET',
                url : 'newsletter.php',
                data: {
                    name : $('#os0').val(),
                    email: $('#os1').val()
                },
                success : function(data) {
                    $('#subscribe').submit();
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {
                    // error handling
                }
            });
            return false;
        }
        return true;
    });
});
</script>

HTML

<form name="signup" id="signup" action="" method="GET">
    <input type="text" id="os0" name="Email" />
    <input class="text" id="os1" type="text" name="cd_FULLNAME" />
    <input type="Submit" name="Submit" id="subscribe" value="Subscribe" />
</form>

Upvotes: 0

Views: 81

Answers (1)

Igor
Igor

Reputation: 15893

<script type='text/javascript'>
$(function () {
    $("#signup").on('submit', function( e, response ) {
        $.ajax({
            type : 'GET',
            url : 'newsletter.php',
            data: {
                name : $('#os0').val(),
                email: $('#os1').val()
            },
            success : function(data) {
                $("#signup")[0].submit();
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                // error handling
            }
        });
        return false;
    });
});
</script>

Note the difference between $("#signup")submit(); and $("#signup")[0].submit();.

Upvotes: 1

Related Questions