Boostrap and two buttons

Asked

Viewed 42 times

-1

I’m trying to log in and register on a page, but when I click on the sign up button, it goes back to the beginning of the login.php. Where do I leave the register_form in display: none, not showing blank field errors.

Anyone have a suggestion? Thank you

Filing cabinet login.php

<?php include_once 'libs/login_user.php';?>
<!doctype html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="favicon.ico">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link href="style.css" rel="stylesheet">
  </head>

  <body class="login">
    <?php include 'static/header.php'; ?>

    <div class="login_form">
    <div class="container">
      <h1>Entrar</h1>
      <div class="col-lg-4 well"> 
        <?php if(isset($errorLogin)) { echo 
          '<div class="alert alert-danger" role="alert">
          <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
          <span class="sr-only">Error:</span>'.$errorLogin.
          '</div>'
        ;}?> 
        <div class="row">
          <form  method="POST" action="login.php">
            <div class="col-sm-12">
              <div class="form-group">
                <label>E-mail</label>
                <div class="input-group">
                  <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
                  <input type="text" class="form-control" id="loginEmail" name="loginEmail">
                </div>
              </div>    
              <div class="form-group">
                <label>Senha</label>
                  <div class="input-group">
                    <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>
                    <input type="text" class="form-control" id="loginPassword" name="loginPassword">
                  </div>
              </div>    
              <input type="submit" name="buttonLogin" id="buttonLogin" value="Entrar" class="btn btn-lg btn-info"></input>
              <label>ou </br></label><label><a id="showRegister">Criar conta</a></label>
            </div>
          </form> 
        </div>
       </div>
    </div>
    </div>

    <div class="register_form">
      <div class="container">
        <h1>Cadrastre-se</h1>
        <div class="col-lg-6 well">
          <?php if(isset($errorRegister)) { echo 
              '<div class="alert alert-danger" role="alert">
              <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
              <span class="sr-only">Error:</span>'.$errorRegister.'
              </div>'
          ;}?> 
          <div class="row">
            <form method="POST" action="login.php">
              <div class="col-sm-12">
                <div class="row">
                  <div class="col-sm-6 form-group">
                    <label>Nome</label>
                    <input type="text" class="form-control" id="registerName" name="registerName">
                  </div>

                  <div class="col-sm-6 form-group">
                    <label>Sobrenome</label>
                    <input type="text" class="form-control" id="registerLastName" name="registerLastName">
                  </div>
                </div>  

              <div class="form-group">
                <label>E-mail</label>
                <input type="email" class="form-control" id="registerEmail" name="registerEmail">
              </div>    

              <div class="row">
                <div class="col-sm-6 form-group">
                  <label>Senha</label>
                  <input type="password" class="form-control" id="registerPassword" name="registerPassword">
                </div>      
                <div class="col-sm-6 form-group">
                  <label>Confirmar Senha</label>
                  <input type="password" class="form-control" id="registerRePassword" name="registerRePassword">
                </div>  
              </div>    
              <input type="submit" name="buttonRegister" id="buttonRegister" value="Cadrastre-me" class="btn btn-lg btn-info"></input>
              <label>ou </br></label><label><a id="showLogin">Entrar</a></label>
            </div>
          </form> 
        </div>
      </div>
    </div>
  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    <script type="text/javascript">
      $(function(){
        $('#showRegister').click(function() {
            $('.login_form').hide();
            $('.register_form').show();
            return false;
        });

        $('#showLogin').click(function() {
            $('.register_form').hide();
            $('.login_form').show();
            return false;
        });
      });
    </script>
  </body>
</html>

Filing cabinet login_users.php

<?php
if (isset($_POST['buttonRegister'])) {
    $name = $_POST['registerName'];
    $lastName = $_POST['registerLastName'];
    $email = $_POST["registerEmail"];
    $password = $_POST["registerPassword"];
    $rePassword = $_POST['registerRePassword'];

    if (empty($name) || empty($lastName) || empty($email) || empty($password) || empty($rePassword)) {
        $errorRegister = "<b>Nome</b>, <b>Sobrenome</b>, <b>Email</b>, <b>Senha</b> e <b>Confirmar Senha</b> são necessários.";
    }
 } 

if (isset($_POST['buttonLogin'])) {
    $email = $_POST["loginEmail"];
    $password = $_POST["loginPassword"];

    if (empty($email) && empty($password))
        $errorLogin = "<b>E-mail</b> e <b>Senha</b> estão em branco";
    else if (empty($email))
       $errorLogin = '<b>E-mail</b> está em branco';
    else if (empty($password))
       $errorLogin = '<b>Senha</b> está em branco';
} 
  • I tested your code, what I did to work was put all the code on the same page, which in this case was login.php

  • Yes, but I would like to leave it in separate files for better code management.

  • Here being on the same page, it still prints the error, but back to the display: None

1 answer

0

If you can put everything in the same file so you will be able to run

<?php 

if (isset($_POST['buttonRegister'])) {
    $name = $_POST['registerName'];
    $lastName = $_POST['registerLastName'];
    $email = $_POST["registerEmail"];
    $password = $_POST["registerPassword"];
    $rePassword = $_POST['registerRePassword'];

    if (empty($name) || empty($lastName) || empty($email) || empty($password) || empty($rePassword)) {
        $errorRegister = "<b>Nome</b>, <b>Sobrenome</b>, <b>Email</b>, <b>Senha</b> e <b>Confirmar Senha</b> são necessários.";
    }
}

if (isset($_POST['buttonLogin'])) {
    $email = $_POST["loginEmail"];
    $password = $_POST["loginPassword"];

    if (empty($email) && empty($password))
        $errorLogin = "<b>E-mail</b> e <b>Senha</b> estão em branco";
        else if (empty($email))
            $errorLogin = '<b>E-mail</b> está em branco';
            else if (empty($password))
                $errorLogin = '<b>Senha</b> está em branco';
} 

?>

<!DOCTYPE html>
<html lang="pt-br">
    <head>

        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

    </head>
    <body>

            <div class="login_form">
                <div class="container">
                  <h1>Entrar</h1>
                  <div class="col-lg-4 well"> 
                    <?php if(isset($errorLogin)) { echo 
                      '<div class="alert alert-danger" role="alert">
                      <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
                      <span class="sr-only">Error:</span>'.$errorLogin.
                      '</div>'
                    ;}?> 
                    <div class="row">
                      <form  method="POST" action="login.php">
                        <div class="col-sm-12">
                          <div class="form-group">
                            <label>E-mail</label>
                            <div class="input-group">
                              <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
                              <input type="text" class="form-control" id="loginEmail" name="loginEmail">
                            </div>
                          </div>    
                          <div class="form-group">
                            <label>Senha</label>
                              <div class="input-group">
                                <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>
                                <input type="text" class="form-control" id="loginPassword" name="loginPassword">
                              </div>
                          </div>    
                          <input type="submit" name="buttonLogin" id="buttonLogin" value="Entrar" class="btn btn-lg btn-info"></input>
                          <label>ou </br></label><label><a id="showRegister">Criar conta</a></label>
                        </div>
                      </form> 
                    </div>
                   </div>
                </div>
                </div>

                <div class="register_form">
                  <div class="container">
                    <h1>Cadrastre-se</h1>
                    <div class="col-lg-6 well">
                      <?php if(isset($errorRegister)) { echo 
                          '<div class="alert alert-danger" role="alert">
                          <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
                          <span class="sr-only">Error:</span>'.$errorRegister.'
                          </div>'
                      ;}?> 
                      <div class="row">
                        <form method="POST" action="login.php">
                          <div class="col-sm-12">
                            <div class="row">
                              <div class="col-sm-6 form-group">
                                <label>Nome</label>
                                <input type="text" class="form-control" id="registerName" name="registerName">
                              </div>

                              <div class="col-sm-6 form-group">
                                <label>Sobrenome</label>
                                <input type="text" class="form-control" id="registerLastName" name="registerLastName">
                              </div>
                            </div>  

                          <div class="form-group">
                            <label>E-mail</label>
                            <input type="email" class="form-control" id="registerEmail" name="registerEmail">
                          </div>    

                          <div class="row">
                            <div class="col-sm-6 form-group">
                              <label>Senha</label>
                              <input type="password" class="form-control" id="registerPassword" name="registerPassword">
                            </div>      
                            <div class="col-sm-6 form-group">
                              <label>Confirmar Senha</label>
                              <input type="password" class="form-control" id="registerRePassword" name="registerRePassword">
                            </div>  
                          </div>    
                          <input type="submit" name="buttonRegister" id="buttonRegister" value="Cadrastre-me" class="btn btn-lg btn-info"></input>
                          <label>ou </br></label><label><a id="showLogin">Entrar</a></label>
                        </div>
                      </form> 
                    </div>
                  </div>
                </div>
        <script>

          $(function(){
                $('#showRegister').click(function() {
                    $('.login_form').hide();
                    $('.register_form').show();
                    return false;
                });

                $('#showLogin').click(function() {
                    $('.register_form').hide();
                    $('.login_form').show();
                    return false;
                });
              });

        </script>
    </body>
</html>

Or you give a include in login.php (usually at the top of the code) like this :

<?php
include 'login_users.php';
?>
<div class="login_form">
<div class="container">
  <h1>Entrar</h1> 
   // Resto do codigo ...
  • But I already have include on the login page.php. I haven’t been able to resolve it yet.

  • Here being on the same page, it still prints the error, but back to the display: None

Browser other questions tagged

You are not signed in. Login or sign up in order to post.