Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved responsive and validation #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 48 additions & 78 deletions create.php
Original file line number Diff line number Diff line change
@@ -1,65 +1,38 @@


<?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;
$validacao = true;

$requiredField = (object)[
"nome" => "Por favor digite o seu nome!",
"endereco" => "Por favor digite o seu endereço!",
"telefone" => "Por favor digite o número do telefone!",
"email" => "Por favor digite um endereço de email válido!",
"sexo" => "Por favor selecione um campo!",
];

if (!empty($_POST) && $_POST = filter_var_array($_POST, FILTER_SANITIZE_STRIPPED)) {
foreach ($requiredField as $field => $msgErro) {
if (isset($_POST[$field]) && !empty($_POST[$field])) {
$$field = $_POST[$field];
$erro[$field] = null;
if ($field == 'email' && !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$erro[$field] = 'Por favor digite um endereço de email válido!';
$validacao = false;
}
} else {
$erro[$field] = $msgErro;
$$field = null;
$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;
}
$erro = (object) $erro;
}

//Inserindo no Banco:
//Inserindo no Banco:
if ($validacao) {
$pdo = Banco::conectar();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Expand All @@ -72,7 +45,6 @@
}
?>


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

Expand All @@ -85,75 +57,75 @@

<body>
<div class="container">
<div clas="span10 offset1">
<div class="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 ' : ''; ?>">
<div class="control-group <?= !empty($erro->nome) ? '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>
value="<?= !empty($nome) ? $nome : ''; ?>">
<?php if (!empty($erro->nome)): ?>
<span class="text-danger"><?= $erro->nome; ?></span>
<?php endif; ?>
</div>
</div>

<div class="control-group <?php echo !empty($enderecoErro) ? 'error ' : ''; ?>">
<div class="control-group <?= !empty($erro->endereco) ? '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>
value="<?= !empty($endereco) ? $endereco : '' ?>">
<?php if (!empty($erro->endereco)): ?>
<span class="text-danger"><?= $erro->endereco; ?></span>
<?php endif; ?>
</div>
</div>

<div class="control-group <?php echo !empty($telefoneErro) ? 'error ' : ''; ?>">
<div class="control-group <?= !empty($erro->telefone) ? '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>
value="<?= !empty($telefone) ? $telefone : ''; ?>">
<?php if (!empty($erro->telefone)): ?>
<span class="text-danger"><?= $erro->telefone; ?></span>
<?php endif; ?>
</div>
</div>

<div class="control-group <?php !empty($emailErro) ? '$emailErro ' : ''; ?>">
<div class="control-group <?= !empty($erro->email) ? 'erro' : ''; ?>">
<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>
value="<?= !empty($email) ? $email : ''; ?>">
<?php if (!empty($erro->email)): ?>
<span class="text-danger"><?= $erro->email; ?></span>
<?php endif; ?>
</div>
</div>

<div class="control-group <?php !empty($sexoErro) ? 'echo($sexoErro)' : ''; ?>">
<div class="control-group <?= !empty($erro->sexo) ? 'erro' : ''; ?>">
<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; ?>/>
value="M" <?= !empty($sexo) && $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; ?>/>
value="F" <?= !empty($sexo) && $sexo == "F" ? "checked" : null; ?>/>
Feminino</p>
</div>
<?php if (!empty($sexoErro)): ?>
<span class="help-inline text-danger"><?php echo $sexoErro; ?></span>
<?php if (!empty($erro->sexo)): ?>
<span class="help-inline text-danger"><?= $erro->sexo; ?></span>
<?php endif; ?>
</div>
</div>
Expand All @@ -167,7 +139,6 @@
</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"
Expand All @@ -177,5 +148,4 @@
<script src="assets/js/bootstrap.min.js"></script>
</body>

</html>

</html>
35 changes: 17 additions & 18 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="assets/css/bootstrap.min.css">
<title>Página Inicial</title>
Expand Down Expand Up @@ -38,24 +39,22 @@
$pdo = Banco::conectar();
$sql = 'SELECT * FROM pessoa ORDER BY id DESC';

foreach($pdo->query($sql)as $row)
{
echo '<tr>';
echo '<th scope="row">'. $row['id'] . '</th>';
echo '<td>'. $row['nome'] . '</td>';
echo '<td>'. $row['endereco'] . '</td>';
echo '<td>'. $row['telefone'] . '</td>';
echo '<td>'. $row['email'] . '</td>';
echo '<td>'. $row['sexo'] . '</td>';
echo '<td width=250>';
echo '<a class="btn btn-primary" href="read.php?id='.$row['id'].'">Info</a>';
echo ' ';
echo '<a class="btn btn-warning" href="update.php?id='.$row['id'].'">Atualizar</a>';
echo ' ';
echo '<a class="btn btn-danger" href="delete.php?id='.$row['id'].'">Excluir</a>';
echo '</td>';
echo '</tr>';
}
foreach($pdo->query($sql)as $row) : ?>
<tr>
<th scope="row"><?= $row['id'] ?></th>
<td><?= $row['nome'] ?></td>
<td><?= $row['endereco'] ?></td>
<td><?= $row['telefone'] ?></td>
<td><?= $row['email'] ?></td>
<td><?= $row['sexo'] ?></td>
<td width=250>
<a class="btn btn-primary" href="read.php?id='.$row['id'].'">Info</a>
<a class="btn btn-warning" href="update.php?id='.$row['id'].'">Atualizar</a>
<a class="btn btn-danger" href="delete.php?id='.$row['id'].'">Excluir</a>
</td>
</tr>
<?php
endforeach;
Banco::desconectar();
?>
</tbody>
Expand Down
Loading