Problem with AJAX Request

1

I'm making a user login system. When the user submits the form the AJAX request is not working and the page changes to login.php

JS

$("form").submit(function() {

    if ($("#login_username").value() != "" && $("#login_password").value() != "") {

        $.ajax({
           url: $("#login-form").attr('action'), //login.php
           data: $("#login-form :input").serializeArray(),
           method: $("#login-form").attr('method'), //post
           success: function(data) {
               alert(data);
           }
        }); 
    }
    else {
        $("#text-login-msg").text("Digite o Usuário e senha").css('color', 'red');
    }
    return false;   
});

It seems that return false is not working

    
asked by anonymous 04.09.2016 / 19:04

1 answer

5

return false; only works in inline JavaScript in HTML.
For example ( link ):

<form onsubmit="return enviar();">

To stop within an event handler, which is your case, you have to use .preventDefault(); .

In your case, it would look like this:

$("form").submit(function(e) {
    e.preventDefault();

However, if you are not going to send form , you could associate the event handset with a button or another element and call ajax, without having to send the form. Suggestion only.

    
04.09.2016 / 19:25