Skip to content
Advertisement

How to use two forms in the Ajax request?

var getLoginpasssystem = function(getPassForgotSystem,getLoginCheckSystem){
$(document).ready(function() {
    $('#login' || '#lostpasswordform').submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: 'http://www.virtuelles-museum.com.udev/spielelogin/logsystem.php',
            data: $(this).serialize(),
            success: function(response) {
                var data = JSON.parse(response);

                if (data.success == "accepted") {
                    document.getElementById('inner').innerHTML = 'Herzlich Willkommen';

                    // location.href = 'index.php';
                } else {
                    alert('Ungültige Email oder Password!');
                }
            }
        });
    });
})
}

The question is how to use two forms in one request with ajax. In this code I used ||, but it doesn’t work. I mean the #login form works well but the #lostpasswordform doesn’t work. When I click on the button it reloads the page instead of giving an alert.

Advertisement

Answer

The reason for this is the way you do your jQuery selection. Selecting multiple elements is done like this: $( "div, span, p.myClass" )

In other words it should work if you replace $('#login' || '#lostpasswordform') with $('#login, #lostpasswordform')

You can read more in detail about this in the jQuery docs

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement