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

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?