I'm trying to login and record to a page, but when I click the button to register, it returns to the beginning of login.php
. Where I leave register_form
in display: none
, not showing blank field errors.
Does anyone have a suggestion? Thanks
Login.php file
<?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><scriptsrc="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>
Login_users.php file
<?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';
}