Andrew
Andrew

Reputation: 12442

Create a jQuery variable from a PHP script?

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>"); }
        });
  });

Upvotes: 0

Views: 223

Answers (2)

kolypto
kolypto

Reputation: 35533

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

Upvotes: 2

jitter
jitter

Reputation: 54625

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");

Upvotes: 4

Related Questions