Doe Chalie
Doe Chalie

Reputation: 15

show value of php variable on some place

I wrote a code to show the return variable value at some specific location in HTML paragraph tag without using AJAX.

I wrote below code for that but its not working for me.

<?php
$Success = "";
$Failed = "";
?>

<br><br>

<center>
    <h3>Already a user? Log in:</h3>
    <p style="color:green;"><?php echo $Success; ?></p>
    <p style="color:red;"><?php echo $Failed; ?></p>
    <form action="" method="POST">
        <div>Username:</div> <input type="text" name="username">
        <div>Password:</div> <input type="password" name="password">
        <br><br>
        <input type="submit" value="Log In" name="login">
    </form>
</center>

<?php
if(isset($_POST['login']))
{
    if( !empty($_POST['username']) && !empty($_POST['password']) )
    {
        $Success = "Thanks.";
    }
    else
    {
        $Failed = "You must supply a username and password.";
    }
}
?>

Upvotes: 0

Views: 41

Answers (1)

Mohammad Hamedani
Mohammad Hamedani

Reputation: 3354

Php scripts compiled line by line, So you must assign variable before using those. You can use this:

<?php
$Success = "";
$Failed = "";

if(isset($_POST['login']))
{
    if( !empty($_POST['username']) && !empty($_POST['password']) )
    {
        $Success = "Thanks.";
    }
    else
    {
        $Failed = "You must supply a username and password.";
    }
}
?>

<br><br>

<center>
    <h3>Already a user? Log in:</h3>
    <p style="color:green;"><?php echo $Success; ?></p>
    <p style="color:red;"><?php echo $Failed; ?></p>
    <form action="" method="POST">
        <div>Username:</div> <input type="text" name="username">
        <div>Password:</div> <input type="password" name="password">
        <br><br>
        <input type="submit" value="Log In" name="login">
    </form>
</center>

Upvotes: 1

Related Questions