Skip to content

Commit

Permalink
Merge pull request glaucia86#6 from dotch3/master
Browse files Browse the repository at this point in the history
Cambios na validacao dos forms, utilizando class text danger para os …
  • Loading branch information
glaucia86 authored Jun 3, 2020
2 parents 5bbdb18 + 6b15ef3 commit 1b8d041
Show file tree
Hide file tree
Showing 3 changed files with 348 additions and 333 deletions.
296 changes: 156 additions & 140 deletions create.php
Original file line number Diff line number Diff line change
@@ -1,165 +1,181 @@


<?php
require 'banco.php';
//Acompanha os erros de validação

// Processar so quando tenha uma chamada post
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$nomeErro = null;
$enderecoErro = null;
$telefoneErro = null;
$emailErro = null;
$sexoErro = null;

if (!empty($_POST)) {
$validacao = True;
$novoUsuario = False;
if (!empty($_POST['nome'])) {
$nome = $_POST['nome'];
} else {
$nomeErro = 'Por favor digite o seu nome!';
$validacao = False;
}


if (!empty($_POST['endereco'])) {
$endereco = $_POST['endereco'];
} else {
$enderecoErro = 'Por favor digite o seu endereço!';
$validacao = False;
}


if (!empty($_POST['telefone'])) {
$telefone = $_POST['telefone'];
} else {
$telefoneErro = 'Por favor digite o número do telefone!';
$validacao = False;
}


if (!empty($_POST['email'])) {
$email = $_POST['email'];
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$emailErro = 'Por favor digite um endereço de email válido!';
$validacao = False;
}
} else {
$emailErro = 'Por favor digite um endereço de email!';
$validacao = False;
}


if (!empty($_POST['sexo'])) {
$sexo = $_POST['sexo'];
} else {
$sexoErro = 'Por favor seleccione um campo!';
$validacao = False;
}
}

//Inserindo no Banco:
if ($validacao) {
$pdo = Banco::conectar();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO pessoa (nome, endereco, telefone, email, sexo) VALUES(?,?,?,?,?)";
$q = $pdo->prepare($sql);
$q->execute(array($nome, $endereco, $telefone, $email, $sexo));
Banco::desconectar();
header("Location: index.php");
}
}
?>


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

<head>
<meta charset="utf-8">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="assets/css/bootstrap.min.css">
<title>Adicionar Contato</title>
<link rel="stylesheet" href="assets/css/bootstrap.min.css">
<title>Adicionar Contato</title>
</head>

<body>
<div class="container">
<div clas="span10 offset1">
<div class="card">
<div class="container">
<div clas="span10 offset1">
<div class="card">
<div class="card-header">
<h3 class="well"> Adicionar Contato </h3>
</div>
<div class="card-body">
<form class="form-horizontal" action="create.php" method="post">

<div class="control-group <?php echo !empty($nomeErro)?'error ' : '';?>">
<label class="control-label">Nome</label>
<div class="controls">
<input size="50" class="form-control" name="nome" type="text" placeholder="Nome" required="" value="<?php echo !empty($nome)?$nome: '';?>">
<?php if(!empty($nomeErro)): ?>
<span class="help-inline"><?php echo $nomeErro;?></span>
<?php endif;?>
</div>
</div>

<div class="control-group <?php echo !empty($enderecoErro)?'error ': '';?>">
<label class="control-label">Endereço</label>
<div class="controls">
<input size="80" class="form-control" name="endereco" type="text" placeholder="Endereço" required="" value="<?php echo !empty($endereco)?$endereco: '';?>">
<?php if(!empty($emailErro)): ?>
<span class="help-inline"><?php echo $enderecoErro;?></span>
<?php endif;?>
</div>
</div>

<div class="control-group <?php echo !empty($telefoneErro)?'error ': '';?>">
<label class="control-label">Telefone</label>
<div class="controls">
<input size="35" class="form-control" name="telefone" type="text" placeholder="Telefone" required="" value="<?php echo !empty($telefone)?$telefone: '';?>">
<?php if(!empty($emailErro)): ?>
<span class="help-inline"><?php echo $telefoneErro;?></span>
<?php endif;?>
</div>
</div>

<div class="control-group <?php echo !empty($emailErro)?'error ': '';?>">
<label class="control-label">Email</label>
<div class="controls">
<input size="40" class="form-control" name="email" type="text" placeholder="Email" required="" value="<?php echo !empty($email)?$email: '';?>">
<?php if(!empty($emailErro)): ?>
<span class="help-inline"><?php echo $emailErro;?></span>
<?php endif;?>
<form class="form-horizontal" action="create.php" method="post">

<div class="control-group <?php echo !empty($nomeErro) ? 'error ' : ''; ?>">
<label class="control-label">Nome</label>
<div class="controls">
<input size="50" class="form-control" name="nome" type="text" placeholder="Nome"
value="<?php echo !empty($nome) ? $nome : ''; ?>">
<?php if (!empty($nomeErro)): ?>
<span class="text-danger"><?php echo $nomeErro; ?></span>
<?php endif; ?>
</div>
</div>
</div>

<div class="control-group <?php echo !empty($sexoErro)?'error ': '';?>">
<label class="control-label">Sexo</label>
<div class="controls">
<div class="form-check">
<p class="form-check-label">
<input class="form-check-input" type="radio" name="sexo" id="sexo" value="M" <?php echo ($sexo=="M" ) ? "checked" : null; ?>/> Masculino

<div class="control-group <?php echo !empty($enderecoErro) ? 'error ' : ''; ?>">
<label class="control-label">Endereço</label>
<div class="controls">
<input size="80" class="form-control" name="endereco" type="text" placeholder="Endereço"
value="<?php echo !empty($endereco) ? $endereco : ''; ?>">
<?php if (!empty($emailErro)): ?>
<span class="text-danger"><?php echo $enderecoErro; ?></span>
<?php endif; ?>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="sexo" id="sexo" value="F" <?php echo ($sexo=="F" ) ? "checked" : null; ?>/> Feminino
</div>

<div class="control-group <?php echo !empty($telefoneErro) ? 'error ' : ''; ?>">
<label class="control-label">Telefone</label>
<div class="controls">
<input size="35" class="form-control" name="telefone" type="text" placeholder="Telefone"
value="<?php echo !empty($telefone) ? $telefone : ''; ?>">
<?php if (!empty($telefoneErro)): ?>
<span class="text-danger"><?php echo $telefoneErro; ?></span>
<?php endif; ?>
</div>
</p>
<?php if(!empty($sexoErro)): ?>
<span class="help-inline"><?php echo $sexoErro;?></span>
<?php endif;?>
</div>
</div>
<div class="form-actions">
<br/>

<button type="submit" class="btn btn-success">Adicionar</button>
<a href="index.php" type="btn" class="btn btn-default">Voltar</a>
<div class="control-group <?php !empty($emailErro) ? '$emailErro ' : ''; ?>">
<label class="control-label">Email</label>
<div class="controls">
<input size="40" class="form-control" name="email" type="text" placeholder="Email"
value="<?php echo !empty($email) ? $email : ''; ?>">
<?php if (!empty($emailErro)): ?>
<span class="text-danger"><?php echo $emailErro; ?></span>
<?php endif; ?>
</div>
</div>

</div>
</form>
</div>
</div>
<div class="control-group <?php !empty($sexoErro) ? 'echo($sexoErro)' : ''; ?>">
<div class="controls">
<label class="control-label">Sexo</label>
<div class="form-check">
<p class="form-check-label">
<input class="form-check-input" type="radio" name="sexo" id="sexo"
value="M" <?php isset($_POST["sexo"]) && $_POST["sexo"] == "M" ? "checked" : null; ?>/>
Masculino</p>
</div>
<div class="form-check">
<p class="form-check-label">
<input class="form-check-input" id="sexo" name="sexo" type="radio"
value="F" <?php isset($_POST["sexo"]) && $_POST["sexo"] == "F" ? "checked" : null; ?>/>
Feminino</p>
</div>
<?php if (!empty($sexoErro)): ?>
<span class="help-inline text-danger"><?php echo $sexoErro; ?></span>
<?php endif; ?>
</div>
</div>
<div class="form-actions">
<br/>
<button type="submit" class="btn btn-success">Adicionar</button>
<a href="index.php" type="btn" class="btn btn-default">Voltar</a>
</div>
</form>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="assets/js/bootstrap.min.js"></script>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"
integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
crossorigin="anonymous"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="assets/js/bootstrap.min.js"></script>
</body>

</html>

<?php
require 'banco.php';

if(!empty($_POST))
{
//Acompanha os erros de validação
$nomeErro = null;
$enderecoErro = null;
$telefoneErro = null;
$emailErro = null;
$sexoErro = null;

$nome = $_POST['nome'];
$endereco = $_POST['endereco'];
$telefone = $_POST['telefone'];
$email = $_POST['email'];
$sexo = $_POST['sexo'];

//Validaçao dos campos:
$validacao = true;
if(empty($nome))
{
$nomeErro = 'Por favor digite o seu nome!';
$validacao = false;
}

if(empty($endereco))
{
$enderecoErro = 'Por favor digite o seu endereço!';
$validacao = false;
}

if(empty($telefone))
{
$telefoneErro = 'Por favor digite o número do telefone!';
$validacao = false;
}

if(empty($email))
{
$emailError = 'Por favor digite o endereço de email';
$validacao = false;
}
elseif (!filter_var($email,FILTER_VALIDATE_EMAIL))
{
$emailError = 'Por favor digite um endereço de email válido!';
$validacao = false;
}

if(empty($sexo))
{
$sexoErro = 'Por favor digite o campo!';
$validacao = false;
}

//Inserindo no Banco:
if($validacao)
{
$pdo = Banco::conectar();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO pessoa (nome, endereco, telefone, email, sexo) VALUES(?,?,?,?,?)";
$q = $pdo->prepare($sql);
$q->execute(array($nome,$endereco,$telefone,$email,$sexo));
Banco::desconectar();
header("Location: index.php");
}
}
?>
Loading

0 comments on commit 1b8d041

Please sign in to comment.