-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
151 lines (150 loc) · 3.89 KB
/
index.php
File metadata and controls
151 lines (150 loc) · 3.89 KB
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
<!DOCTYPE html>
<html lang="pt-br">
<head>
<title> Desafio CPT</title>
<style>
.titulo{
text-align: center;
font-family: sans-serif;
}
.formulario{
margin:auto;
width:50%;
border:3px solid black;
border-radius: 10px;
padding:10px;
background-color: #DCDCDC;
}
.form{
margin:10px;
}
input, textarea {
display: block;
width:98%;
padding:8px;
}
.enviar{
width:45%;
background-color:#228B22;
border-radius:10px;
font-family:bold;
font-color:yellow;
border: 1px solid transparent;
font-size: 20px;
cursor:pointer;
color:#F8F8FF;
float: left;
margin:10px;
}
.reset{
width:45%;
background-color:#B22222;
border-radius:10px;
font-family:bold;
font-color:yellow;
border: 1px solid transparent;
font-size: 20px;
cursor:pointer;
color:#F8F8FF;
float: center;
margin:10px;
}
</style>
<script>
//Mascara de telefone
function mascara(o,f){
v_obj=o
v_fun=f
setTimeout("execmascara()",1)
}
function execmascara(){
v_obj.value=v_fun(v_obj.value)
}
function mtel(v){
v=v.replace(/\D/g,""); //Remove tudo o que não é dígito
v=v.replace(/^(\d{2})(\d)/g,"($1) $2"); //Coloca parênteses em volta dos dois primeiros dígitos
v=v.replace(/(\d)(\d{4})$/,"$1-$2"); //Coloca hífen entre o quarto e o quinto dígitos
return v;
}
function id( el ){
return document.getElementById( el );
}
window.onload = function(){
id('telefone').onkeyup = function(){
mascara( this, mtel );
}
}
</script>
</head>
<body>
<div>
<h2 class="titulo"> Fomulário de Cadastro</h2>
</div>
<div class="formulario">
<form method="post" action="">
<div class="form">
<label> Nome</label>
<input type="text" name="nome" required>
</div>
<div class="form">
<label> E-mail </label>
<input type="email" name="email" required>
</div>
<div class="form">
<label>Telefone</label>
<input type="text" name="telefone" placeholder="Insira o numero com o ddd" id="telefone" maxlength="15" required>
</div>
<div class="form">
<label> Assunto</label>
<input type="text" name="assunto" required>
</div>
<div class="form">
<label> Mensagem</label>
<textarea name="mensagem"></textarea>
</div>
<div class="form">
<button type="submit" name="enviar" class="enviar">Enviar Dados</button>
<button type="reset" class="reset"> Apagar Dados</button>
</div>
</form>
</div>
</body>
</html>
<?php
if(isset($_POST['enviar'])){
//conexão com o banco de dados
$username="root";
$password="";
try {
$conn = new PDO('mysql:host=localhost;dbname=cpt', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
//pegando os dados do formulario
$nome = $_POST['nome'];
$email = $_POST['email'];
$telefone = $_POST['telefone'];
$assunto = $_POST['assunto'];
$mensagem = $_POST['mensagem'];
//inserindo no banco de dados
$sql= $conn->prepare("INSERT INTO `tbl_contato`( `nome`, `email`, `telefone`, `assunto`, `mensagem`) VALUES(:nome,:email,:telefone, :assunto,:mensagem)");
$sql->bindParam('nome', $nome, PDO::PARAM_STR);
$sql->bindParam('email', $email, PDO::PARAM_STR);
$sql->bindParam('telefone', $telefone, PDO::PARAM_STR);
$sql->bindParam('assunto', $assunto, PDO::PARAM_STR);
$sql->bindParam('mensagem', $mensagem, PDO::PARAM_STR);
$sql->execute();
if($sql->rowCount()){
echo "<script language='javascript'>";
echo 'alert("Cadastro realizado com sucesso!!");';
echo 'window.location.replace("index.php");';
echo "</script>";
}else{
echo "<script language='javascript'>";
echo 'alert("Cadastro não realizado!!");';
echo 'window.location.replace("index.php");';
echo "</script>";
}
}
?>