0

I have defined a variable with string value in library.php and I want to use it in another page client-login.php without useing an anchor tag.

library.php:

if(isset($_POST['client-login-submit'])){
$username = trim($_POST['username']);
$password = trim($_POST['password']);

if($username == ""){
    $login_error_message = 'Username field is required!';


}else if($password == ""){
    $login_error_message = 'Password field is required!';


}else{
    $id = Login($username, $password);
    if($id > 0){
        $_SESSION['id'] = $id; // Set Session
        header("location: ../../main.php");
    }else{
        $login_error_message = 'Invalid login details!';
        }
    }
}

client-login.php:

<form action="inc/lib/library.php" method="post">

        <h1>LOG IN</h1>
        <input placeholder="Username" type="text" name="username" id="username">
        <input placeholder="Password" type="password" name="password" id="password">
        <input type="submit" name="client-login-submit" value="LOG IN">
        <p>Forget Your Password? <a href="#">RESET</a></p>
        <p><a href="client-sign-up.php">SIGN UP</a></p>
        <?php
        if ($login_error_message != "") {
            echo '<p style="color: red;"><strong>Error: </strong> ' .$login_error_message. '</p>';
        }
        ?>
    </form>

this is the error I get in the place that $login_error_message must be shown after occuring error... :

Notice: Undefined variable: login_error_message in C:\xampp\htdocs\fashion\client-login.php on line 54

after submitting the form if there is an error then it must be shown but the error above is already shown even before clicking... and when I click on submit button it goes to library.php page which is blank.

Would you please help how to use $login_error_message in client-login.php? Thanks in advance...

faro621
  • 3
  • 1
  • please have a look at this questions: https://stackoverflow.com/questions/18588972/how-to-access-a-variable-across-two-files – schildi May 15 '18 at 11:55
  • Set that value in `$_SESSION`? It's not really clear to me what you mean by "without using an anchor tag". But if you have two different pages and you want to set a value on the first one and read that value on the second one then the most common first approach to this would be using session state. – David May 15 '18 at 11:55
  • echo '

    Error: ' .isset($login_error_message)?$login_error_message:' '. '

    ';
    – Sachin Aghera May 15 '18 at 11:55
  • @SachinAghera: That doesn't really solve anything, since the variable is *always undefined*. – David May 15 '18 at 11:57

1 Answers1

0

You can set your variable to a Session variable and access it to your other page.

In library.php:
$_SESSION['login_error_msg'] = 'your msg';

In client-login.php:
if(isset($_SESSION['login_error_msg']))
{
    echo '<p style="color: red;"><strong>Error: </strong> ' .$_SESSION['login_error_msg']. '</p>';
}
Azima
  • 3,835
  • 15
  • 49
  • 95