forked from glaucia86/projeto.crud.php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.php
165 lines (146 loc) · 6.72 KB
/
create.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<!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>
</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" 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;?>
</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>
<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>
</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>
</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>
<!-- 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))
{
$telefoneErro = '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");
}
}
?>