Matta301
Matta301

Reputation: 115

Return a variable from a php function

No doubt there is a very quick and easy solution for this but I can’t seem to get my head around it. How would I go about returning the variable $errMsg from the function below? So I can display it on the page where the function is being called.

function login(){

    global $wpdb;

    if( isset($_POST['login_btn'] )){


        if (empty($_POST['login_username'] || empty($_POST['login_password']))) {

            $errMsg = 'Invalid Username or Password';
            return $errMsg; 

        }else {

            $login_username = $_POST['login_username'];
            $login_password = $_POST['login_password'];

            $u = $login_username;
            $p = $login_password;


            $query = $wpdb -> prepare("SELECT * FROM 'wp_members' WHERE     'username' = %u AND 'password' = %p", $u, $p );

            // If there is a matching username and password then redirected to login
            if ($query == true) {

                $_SESSION['login_username'] = $login_username;          
                header('Location: /flourishWP/wp-content/themes/blankslate/includes/admin.php');    
            }
        }
    }
}

Upvotes: 1

Views: 79

Answers (3)

user5421628
user5421628

Reputation:

Try using echo

    if (empty($_POST['login_username'] || empty($_POST['login_password']))) {

        $errMsg = 'Invalid Username or Password';
        echo $errMsg; 

    }

Upvotes: 1

Zura Tsilosani
Zura Tsilosani

Reputation: 69

$someVar = login(); now inside $someVar you have returned values of your function

Upvotes: 3

Sebastian Schneider
Sebastian Schneider

Reputation: 5532

Very simple:

$errmsg = login();

if($errmsg != null){
    echo $errmsg;
}

You call the function and if there is an error message you display it.

Upvotes: 0

Related Questions