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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.