Skip to main content

How to do a ajax request for login


I have this in my PHP code, and it currently does the login request in the same login.php page but now i want to do it with Ajax. Basically I have this in the login.php




echo '<form method="post" ><div id="login" class="login">
<label for="login">User Name</label>
<input type="text" name="logInUsername" />
<label for="Password">Password</label>
<input type="password" name="logInPassword" />
<input type="submit" value="Submit" name="submitlogin" class="button" />
</div>';



I would like to still use this but have a login_request.php or something where i can send the username and password validated and then change the <div id=login> to say you are logged in!</div> I can do it the conventional way, with the form post .. but now I would like to try it with Ajax.



Any help will be much appreciated.



Regards


Source: Tips4allCCNA FINAL EXAM

Comments

  1. What have you tried so far? This is how I would start:

    This should get you started:

    HTML:

    <form id="loginForm" ><div id="login" class="login">
    <label for="login">User Name</label>
    <input type="text" name="logInUsername" />
    <label for="Password">Password</label>
    <input type="password" name="logInPassword" />
    <input type="button" value="Submit" id="submitlogin" class="button" />
    </div>
    </form>


    jQuery:

    $("#submitlogin").click(function() {

    inputs = //grab then inputs of your form #loginform
    $.ajax ({
    url: "urltoyourloginphp.php",
    data: inputs,
    success: function() {
    $("#login").html("You are now logged in!");
    }
    });
    }

    ReplyDelete
  2. I wrote this a while ago, it's not quite a full ajax login (i.e. at the end it does still redirect you), but it may serve as a basis for a full ajax login. As a plus you actually don't need https (that was the whole point of this little project).

    https://github.com/eberle1080/secure_http_login/blob/master/login.php

    The high level steps go something like this:


    Ask the server for a seed value (a salt) using an ajax request
    Hash the password + seed using a sha1 sum
    Ask the server to verify the username and salted + hashed password
    If it's valid, the server sets a session cookie indicating that the user is logged in
    The server responds to the ajax request with a success / fail message

    ReplyDelete
  3. jQuery has built in .post() and .serialize() methods for wrapping up a form.

    $.post("login.php", $("#loginForm").serialize(), function(data) {
    //pass information back in with data. if it's JSON, use $.parseJSON() to parse it.
    alert('either logged in or errored');
    );


    You will also need to edit your form so it has an id, like: <form id="loginForm">...

    ReplyDelete
  4. I don't know PHP but will give you an example of how I would have done it with vbscript (classic asp) so you may try to adapt it to PHP as needed.

    I, in my applications, don't use the form tag since I first used ajax. So, here we go:

    login html page:

    include jquery
    <script type='text/javascript' src='your-jquery-url'></script>
    <script type='text/javascript'>

    function tryLogin() {
    var inputs='userName='+$('logInUsername').val()+
    '&userPassw='+$('logInPassword').val();
    //notice that I changed your name= to id= in the form
    //notice the '&' in the '&userPassw=
    $.post('your-login-validation-page',inputs,function(data) {
    eval('var json='+data);
    if (json['success'] == 'true') {
    $('#loginForm').html('<p>Congratulations! You\'ve been logged in successfully</p>')
    } else {
    alert(json['errorMessage']);
    $('#logInUsername').focus();
    }
    });
    }

    </script>

    <div id='loginForm' >
    <label for="login">User Name</label>
    <input type="text" id="logInUsername" />
    <label for="Password">Password</label>
    <input type="password" id="logInPassword" />
    <button onClick='tryLogin(); ' >LOGIN</button>
    </div>



    login-validation-page

    [in vbscript]

    user = request.Form("userName")
    passw = request.Form("userPassw")

    "if is there this user" (coded as if there was a database look up...)
    "if the password = passw" (coded as comparing the values)
    response.write "{'sucess':'true'}"
    else
    response.write "{'success':'false','errorMessage':'wrong password'}"
    end if
    else
    response.write "{'success':'false','errorMessage':'user not found'}"
    end if

    ---> end of login-validation-page

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex