create.php

<< Click to Display Table of Contents >>

Navigation:  Outros > CRUD simples com Bootstrap >

create.php

Previous pageReturn to chapter overviewNext page

<?php
require 'banco.php';
 
// limpa o POST (quando usuario pressionar "Adicionar"
$post_array = filter_input_array(INPUT_POSTFILTER_DEFAULT);
 
if ($post_array) {
 
    // mensagens de erros de validação
    $nomeErro = null;
    $enderecoErro = null;
    $telefoneErro = null;
    $emailErro = null;
 
    // transfere o conteudo do POST para variaveis
    if ($post_array) {
        $nome = $post_array['nome'];
        $endereco = $post_array['endereco'];
        $telefone = $post_array['telefone'];
        $email = $post_array['email'];
        $sexo = $post_array['sexo'] ?? "M";
    }
 
    $validacao = true;
 
    // validar todos os campos
    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)) {
        $emailErro = 'Por favor digite o endereço de email';
        $validacao = false;
    } elseif (!filter_var($emailFILTER_VALIDATE_EMAIL)) {
        $emailErro = 'Por favor digite um endereço de email válido!';
        $validacao = false;
    }
 
    //Inserindo no Banco:
    if ($validacao) {
        $pdo = Banco::conectar();
        $pdo->setAttribute(PDO::ATTR_ERRMODEPDO::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="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
        <title>Adicionar Contato</title>
    </head>
    <body>
        <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" value="<?php echo!empty($nome) ? $nome : ''; ?>">
                                    <?php if (!empty($nomeErro)): ?>
                                        <span class="help-inline badge-warning"><?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" value="<?php echo!empty($endereco) ? $endereco : ''; ?>">
                                    <?php if (!empty($enderecoErro)): ?>
                                        <span class="help-inline badge-warning"><?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" value="<?php echo!empty($telefone) ? $telefone : ''; ?>">
                                    <?php if (!empty($telefoneErro)): ?>
                                        <span class="help-inline badge-warning"><?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" value="<?php echo!empty($email) ? $email : ''; ?>">
                                    <?php if (!empty($emailErro)): ?>
                                        <span class="help-inline badge-warning"><?php echo $emailErro; ?></span>
                                    <?php endif; ?>
                                </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!empty($sexo) ? ($sexo == "M" ) ? "checked" : null : "checked"; ?>/> Masculino
                                    </div>
                                    <div class="form-check">
                                        <input class="form-check-input" type="radio" name="sexo" id="sexo" value="F" <?php echo!empty($sexo) ? ($sexo == "F" ) ? "checked" : null : null; ?>/> Feminino
                                    </div>
                                    </p>
                                </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-primary">Voltar</a>
                            </div>
                        </form>
                    </div>
                </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>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</body>
</html>