On successful submission hide login form

Viewed 66

im building an e-commerce site that has a login page, the login page works and give a success or error message depending on if a correct login detail is given and resets the form when needed. but I need the form to become hidden when a successful login has happened. i know that I need an if statement with a PHP tag but that's a far as I've gotten.

this is the code

    <?php
require('includes/application_top.php');
$page_title='Login!';
if (isset($_POST['action'])) {
    $username_input = $_POST['username'];
    $password_input = $_POST['password'];
    $login_ok = login($username_input, $password_input);
}
require('includes/site_header.php');
?>
<style>
<?php
require('css/login.css');
?>
</style>
<br>
<br>
<br>
<?php
        if (isset($login_ok)) {
            ?>
            <div class="row">
                <div class="col-sm-12">
                    <?php
                    if ( ! $login_ok) {
                        ?>
                        <div class="alert alert-danger">
                            <p>Your credentials were invalid</p>
                        </div>
                        <?php
                    } else {
                        ?>
                        <div class="alert alert-success">
                            <p>Login successful</p>
                        </div>
                        <?php
                    }
                    ?>
                </div>
            </div>
            <?php
        }
        ?>
<?php 
      if (isset(! $login_okay)) {
        ?>
        <
      }
<div class="wrapper fadeInDown">
  <div id="formContent">
    <!-- Icon -->
    <div class="fadeIn first">
      <br>
      <img src="images/Head1.jpg" style="height: 40%; width: 60%;" id="icon" alt="User Icon" />
      <br>
      <h3> Login! </h3>
    </div>
    <br>
    <!-- Login Form -->
    <form id="login_form" action="login_page.php" method="post">
      <input type="text" class="fadeIn second" id="username" name="username" placeholder="Username" value="<?= isset($_POST['username']) ? htmlspecialchars($_POST['username']) : '' ?>" maxlength="100" />
      <input type="password" class="form-control" id="password" name="password" placeholder="Password" value="" />
      <input type="submit" class="fadeIn fourth" name="action" value="Login"/>
    </form>

    <!-- Remind Passowrd -->
    <div id="formFooter">
      <a class="underlineHover" href="#">Forgot Password?</a>
    </div>

  </div>
</div>

<?php
require('includes/application_bottom.php');
require('includes/site_footer.php');
?>
1 Answers
Assuming your setting $login_ok = 1 for a pass and $login_ok = 0 for a fail.     



   <?php
require('includes/application_top.php');
$page_title='Login!';
if (isset($_POST['action'])) {
    $username_input = $_POST['username'];
    $password_input = $_POST['password'];
    $login_ok = login($username_input, $password_input); // 1 for ok 0 for failed
}
require('includes/site_header.php');
?>
<style>
<?php
//require('css/login.css');
?>
</style>
<?php
// Part one Create login form 

// Store posted username incase of error 
$insertonfail=isset($_POST['username']) ? htmlspecialchars($_POST['username']) : '';
// Create form 
$loginform="
<div class=\"wrapper fadeInDown\">
  <div id=\"formContent\">
    <!-- Icon -->
    <div class=\"fadeIn first\">
      <br>
      <img src=\"images/Head1.jpg\" style=\"height: 40%; width: 60%;\" id=\"icon\" alt=\"User Icon\" />
      <br>
      <h3> Login! </h3>
    </div>
    <br>
    <!-- Login Form -->
    <form id=\"login_form\" action=\"login_page.php\" method=\"post\">
      <input type=\"text\" class=\"fadeIn second\" id=\"username\" name=\"username\" placeholder=\"Username\" value=\"$insertonfail\" maxlength=\"100\" />
      <input type=\"password\" class=\"form-control\" id=\"password\" name=\"password\" placeholder=\"Password\" value=\"\" />
      <input type=\"submit\" class=\"fadeIn fourth\" name=\"action\" value=\"Login\"/>
    </form>

    <!-- Remind Passowrd -->
    <div id=\"formFooter\">
      <a class=\"underlineHover\" href=\"#\">Forgot Password?</a>
    </div>

  </div>
</div>";
// Check for a login or display form 
if (!isset($login_ok))
    {
    echo $loginform;
    }
// 
if (isset($login_ok))
    {
        // Check passed         
        if ($login_ok == 1)
        {
        echo "<div class=\"row\">
                <div class=\"col-sm-12\">
                <div class=\"alert alert-success\">
                 <p>Login successful</p>
                 </div></div></div>";               
        }           
        // Check Failed  
        if ($login_ok == 0)
          {   
          echo "<div class=\"row\">
                <div class=\"col-sm-12\"><div class=\"alert alert-danger\">
                            <p>Your credentials were invalid</p>
                          </div></div></div>";      
                          
            echo $loginform;                    
          }     

        
    }
echo "</div>";

?>

<?php
require('includes/application_bottom.php');
require('includes/site_footer.php');
?>
Related