Created
May 19, 2025 22:38
-
-
Save uchoamaster/2128885958ca0d9a0e77c7cdec66a481 to your computer and use it in GitHub Desktop.
create projeto agenda com css v2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Incluir arquivo de conexão | |
require_once 'config/database.php'; | |
// Inicializar variáveis | |
$nome = $telefone = $email = ""; | |
$nome_err = $telefone_err = $email_err = ""; | |
// Processar dados do formulário quando for enviado | |
if ($_SERVER["REQUEST_METHOD"] == "POST") { | |
// Validar nome | |
if (empty(trim($_POST["nome"]))) { | |
$nome_err = "Por favor, informe um nome."; | |
} else { | |
$nome = trim($_POST["nome"]); | |
} | |
// Validar telefone | |
if (empty(trim($_POST["telefone"]))) { | |
$telefone_err = "Por favor, informe um telefone."; | |
} else { | |
$telefone = trim($_POST["telefone"]); | |
} | |
// Validar email (opcional) | |
if (!empty(trim($_POST["email"]))) { | |
if (!filter_var(trim($_POST["email"]), FILTER_VALIDATE_EMAIL)) { | |
$email_err = "Por favor, informe um email válido."; | |
} else { | |
$email = trim($_POST["email"]); | |
} | |
} | |
// Verificar erros de entrada antes de inserir no banco de dados | |
if (empty($nome_err) && empty($telefone_err) && empty($email_err)) { | |
// Preparar declaração de inserção | |
$sql = "INSERT INTO contatos (nome, telefone, email) VALUES (?, ?, ?)"; | |
if ($stmt = mysqli_prepare($conn, $sql)) { | |
// Vincular variáveis à declaração preparada como parâmetros | |
mysqli_stmt_bind_param($stmt, "sss", $param_nome, $param_telefone, $param_email); | |
// Definir parâmetros | |
$param_nome = $nome; | |
$param_telefone = $telefone; | |
$param_email = $email; | |
// Tentar executar a declaração preparada | |
if (mysqli_stmt_execute($stmt)) { | |
// Registros criados com sucesso. Redirecionar para a página principal | |
header("location: index.php?success=create"); | |
exit(); | |
} else { | |
echo "Oops! Algo deu errado. Por favor, tente novamente mais tarde."; | |
} | |
// Fechar declaração | |
mysqli_stmt_close($stmt); | |
} | |
} | |
// Fechar conexão | |
mysqli_close($conn); | |
} | |
?> | |
<!DOCTYPE html> | |
<html lang="pt-br"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Adicionar Contato - Agenda Telefônica</title> | |
<link rel="stylesheet" href="assets/css/style.css"> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css"> | |
</head> | |
<body> | |
<header> | |
<div class="container"> | |
<div class="header-content"> | |
<div class="logo"> | |
<i class="fas fa-address-book fa-2x"></i> | |
<h1>Agenda Telefônica</h1> | |
</div> | |
</div> | |
</div> | |
</header> | |
<div class="container"> | |
<h2><i class="fas fa-plus-circle"></i> Adicionar Novo Contato</h2> | |
<div class="form-container"> | |
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> | |
<div class="form-group"> | |
<label for="nome">Nome</label> | |
<input type="text" id="nome" name="nome" value="<?php echo $nome; ?>" placeholder="Digite o nome completo"> | |
<?php if (!empty($nome_err)): ?> | |
<span class="error-text"><?php echo $nome_err; ?></span> | |
<?php endif; ?> | |
</div> | |
<div class="form-group"> | |
<label for="telefone">Telefone</label> | |
<input type="text" id="telefone" name="telefone" value="<?php echo $telefone; ?>" placeholder="Ex: (11) 98765-4321"> | |
<?php if (!empty($telefone_err)): ?> | |
<span class="error-text"><?php echo $telefone_err; ?></span> | |
<?php endif; ?> | |
</div> | |
<div class="form-group"> | |
<label for="email">Email (opcional)</label> | |
<input type="email" id="email" name="email" value="<?php echo $email; ?>" placeholder="Ex: [email protected]"> | |
<?php if (!empty($email_err)): ?> | |
<span class="error-text"><?php echo $email_err; ?></span> | |
<?php endif; ?> | |
</div> | |
<div class="form-buttons"> | |
<button type="submit" class="btn btn-success"> | |
<i class="fas fa-save"></i> Salvar | |
</button> | |
<a href="index.php" class="btn"> | |
<i class="fas fa-arrow-left"></i> Voltar | |
</a> | |
</div> | |
</form> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment