0

I'm working on a login script, and I want to send the input to a PHP script, check it, and then do something if it matches the one I have in the config.php file.

pseudo php code (admin_login.php)

<?php
require("../config.php");
if($_POST['password'] == $password) {
    $showLoginPage = true;
    }
    else {
        $showLoginPage = false;
    }
?> 

jQuery:

 $("#admin_login_submit").click(function(event) {
     event.preventDefault();
     var pass = $("#admin_login_password").val();
        $.post("includes/process/admin_login.php",
                { password: pass },
                 function(data){
                    $("#show_admin_feedback").html(data);
        });

If I could magically create jQuery code, what I want to do would look like this:

$("#admin_login_submit").click(function(event) {
     event.preventDefault();
     var pass = $("#admin_login_password").val();
        $.post("includes/process/admin_login.php",
                { password: pass },
                 function(data){
                    $("#show_admin_feedback").html(data);
                    showLoginPage = $showLoginPage;
        });
            if(showLoginPage == true) {
                $("#main").load("logged_in.php");
            }
  });

Oh, I have $("#show_admin_feedback").html(data); being returned because I was returning "Worked" or "Didn't work" when I was testing it. :-p.

------Fixed. Thanks, jitter!

php

<?php
require("../config.php");
if($_POST['password'] == $password) {
    header('HTTP/1.1 200 OK');
    }
    else {
        header('HTTP/1.1 404 Not Found');
    }

?> 

jQuery

$("#admin_login_submit").click(function(event) {
     event.preventDefault();
     var pass = $("#admin_login_password").val();
        $.ajax({
            type: "POST",
            url: "includes/process/admin_login.php",
            data: "password=" + pass,
            success: function(){
              $("#main").load("logged_in.php"); },
            error: function(){
                $("#admin_login_show").html("<b>Failed Login</b>"); }
        });
  });

2 Answers 2

4

Either just modify your php script to return a http status code of 404 when login fails. This way your callback function won't fire and you remain on the login page. But you would need to come up with something to tell the user the login failed.

Or you consider using the $.ajax() call instead of $.post(). Now you could rewrite your php script to set different HTTP status codes depending on the succeeded/failed login.

e.g. return 200 when successful and 404 when login failed.

Now with the $.ajax() call you can register the success and the errror callback and act accordingly. Set show_admin_feedback in both cases (displaying something which denotes success/fail for user) and if success also calling $("#main").load("logged_in.php");

Sign up to request clarification or add additional context in comments.

2 Comments

Worked beautifully. Thanks! :)
Very nice. I would have gone with returning a JSON object, but I actually like this one better. Although, I would use the 401 - Unauthorized header. Seems more fitting :)
2

Export JSON from your PHP code, and do whatever you like with the objects you get from this JSON :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.