Why is my form posting after ajax post? How do I fix this?

Bocko

So I have this form:

<form method="post" id="login-nav">
        <div class="form-group">
                <label class="sr-only" for="exampleInputEmail2">Email address</label>
                <input type="email" id="email" name="email" class="form-control" id="exampleInputEmail2" placeholder="Email address" required>
        </div>
        <div class="form-group">
                <label class="sr-only" for="exampleInputPassword2">Password</label>
                <input type="password" id="PassWord" name="PassWord" class="form-control" id="exampleInputPassword2" placeholder="Password" required>
                <div class="help-block text-right"><a href="">Zaboravili ste password?</a></div>
        </div>
        <button id="submitit" onclick="GetUser();" class="btn btn-primary btn-block">Sign in</button>
        <div class="checkbox">
            <label>
                <input type="checkbox"> keep me logged-in
            </label>
        </div>
</form>

And when I press on the Sign in button, I send an post and I have my HTML updated correctly for a brief second, and then the page reloads. This is not what I want. I dont want the page to reload, but stay as it is after my ajax call. Here is the javascript/jquery code:

function GetUser() {
var email = $("#email").val();
var password = $("#PassWord").val();

$.post('../phpdatabase/getlogin.php',{ email: email , password : password }, function(data){
  $('#LogNav').html(data);
});
}
DontVoteMeDown

The default behaviour of a submit button is to submit the form, so you have to prevent it.

onclick="GetUser(event);"

This will pass the event to the function callback. Insde your function you have to use:

function GetUser(e) {
    e.preventDefault();

I would suggest you to change the event from the button to the form submit event:

$("#login-nav").on("submit", function(e) {
    var email = $("#email").val();
    var password = $("#PassWord").val();

    $.post('../phpdatabase/getlogin.php',{ email: email , password : password }, function(data){
      $('#LogNav').html(data);
    });

    e.preventDefault();
});

Avoid using inline events into your HTML elements e.g. onclick, onfocus etc...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I fix my faulty HTML form with JavaScript?

Why is my form not submitting or posting

how can i connect my form to ajax and post through it

why ajax value return is undefined with settimeout? how do I fix it?

How do I fix my display after Atom broke it completely?

How to post form with ajax after form validate?

Why is my actionbar not showing and how do I fix it?

Why is my while going in a infinity loop? How do I fix it?

Why are months in my date slicer repeating? How do I fix it?

How do I use jQuery Post/Ajax instead of form submit?

How do I post a form in laravel 5 using ajax?

How do I get the name of a form after a post request in django?

How do I get my ajax POST (preventDefault) to work?

How do i hide a form after submission is succesfull with Ajax/Jquery

How to Ajax POST after a Form Submit

How do I fix my edit form from editing all the articles in my database in Ruby/Rails?

Why is my form not posting to the pivot table?

After javaScript dynamically added some tags to my HTML my design changed as it is, how do i fix this proplem

My image is being rendered after my text how do i fix this

How do you post a form with ajax in php?

If my OEMCP is set to 437 then why is my cmd.exe default codepage 932? and how do I fix this?

Why my output is "()" and how can i fix it?

How can I post full form with AJAX?

How do I fix my code so that when the button is pressed it will only increment the vote count of the corresponding post?

How do I fix my PHP Form Redirecting to upload files to server

How do I fix floating CSS label always being active showing 'invalid input' in my form?

I do not now why my form fails

Why I am getting POST 404 when Calendly is posting data to my endpoint

Why did my socket close after the handshake and how can I fix this?