Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Boa tarde, sou novato em PHP e estou precisando de ajuda.
Quero criar uma página que verifique se o CPF já existe no BD antes de prosseguir para o cadastro.
Valores (CPF) cadastrados no sistema, retornam com sucesso.
O problema são os CPF's que não estão cadastrados, quando envio o formulário, fica uma tela em branco, afetando toda pagina.
//CONEXÃO.PHP
********************************************
<?php
// (!) Conexão com banco de Dados
$host = "localhost";
$user = "root";
$pass = "";
$database = "bd_gloriadecristo";
$conexao = mysql_connect($host, $user, $pass) or die('Não foi possível conectar com BD: ' .mysql_error());
mysql_select_db($database, $conexao) or die('Não foi possível selecionar o banco da dados');
/print "OK"; Testa a conexão. /
?>
//CADASTRO.PHP
*********************************************
<?
include "conexao.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sistema de Gestão Eclesiástico</title>
<style type="text/css">
.textoIndex {
font-family: "Lucida Console", Monaco, monospace;
color: #333;
font-size: 16px;
}
.BoxLogin {
border:1px solid #999;
moz-border-radius:10px;
-webkit-border-radius:10px;}
.estiloBotao {
-webkit-transition: all 0.30s ease-in-out;
-moz-transition: all 0.30s ease-in-out;
-ms-transition: all 0.30s ease-in-out;
-o-transition: all 0.30s ease-in-out;
outline: none;
padding: 3px 0px 3px 3px;
margin: 5px 1px 3px 0px;
border: 1px solid #DDDDDD;
}
.estiloBotao:focus {
box-shadow: 0 0 5px rgba(81, 203, 238, 1);
padding: 3px 0px 3px 3px;
margin: 5px 1px 3px 0px;
border: 1px solid rgba(81, 203, 238, 1);
}
.confirma {
padding:5px 15px; background:#ccc; border:0 none;
cursor:pointer;
-webkit-border-radius: 5px;
border-radius: 5px;
}
.confirma:hover {
background:#0CF;
}
#cadastrarMenu {
float:left;
width: 50%;
}
#esqueciSenha {
float:right;
width: 50%;
}
#footer {
position: absolute;
bottom: 0px;
width: 100%;
height: 60px;
background-color: #CCC;
left: 1px;
}
.mensagemErro {
border: 1px solid;
margin: 10px 0px;
padding:15px 10px 15px 50px;
background-repeat: no-repeat;
background-position: 10px center;
}
.mensagemErro {
color: #D8000C;
background-color: #FFBABA;
background-image: url('imagens/erro.png');
}
</style>
</head>
<body>
<script type="text/javascript">
function validarCPF( cpf ){
var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
if(!filtro.test(cpf))
{
window.alert("CPF inválido. Tente novamente.");
return false;
}
cpf = remove(cpf, ".");
cpf = remove(cpf, "-");
if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
cpf == "88888888888" || cpf == "99999999999")
{
window.alert("CPF inválido. Tente novamente.");
return false;
}
soma = 0;
for(i = 0; i < 9; i++)soma += parseInt(cpf.charAt(i)) * (10 - i);
}
resto = 11 - (soma % 11);
if(resto == 10 || resto == 11)resto = 0;
}window.alert("CPF inválido. Tente novamente.");
return false;
}
soma = 0;
for(i = 0; i < 10; i ++)soma += parseInt(cpf.charAt(i)) * (11 - i);
}
resto = 11 - (soma % 11);
if(resto == 10 || resto == 11)resto = 0;
}
if(resto != parseInt(cpf.charAt(10))){
window.alert("CPF inválido. Tente novamente.");
return false;
}
return true;
}
function remove(str, sub) {
i = str.indexOf(sub);
r = "";
if (i == -1) return str;r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
}
return r;
}
/**
* MASCARA ( mascara(o,f) e execmascara() ) CRIADAS POR ELCIO LUIZ
* elcio.com.br
*/
function mascara(o,f){
v_obj=o
v_fun=f
setTimeout("execmascara()",1)
}
function execmascara(){
v_obj.value=v_fun(v_obj.value)
}
function cpf_mask(v){
v=v.replace(/\D/g,"") //Remove tudo o que não é dígito
v=v.replace(/(\d{3})(\d)/,"$1.$2") //Coloca ponto entre o terceiro e o quarto dígitos
v=v.replace(/(\d{3})(\d)/,"$1.$2") //Coloca ponto entre o setimo e o oitava dígitos
v=v.replace(/(\d{3})(\d)/,"$1-$2") //Coloca ponto entre o decimoprimeiro e o decimosegundo dígitos
return v
}
</script>
<p align="center"><img src="imagens/logo.jpg" alt="Bem-vindo ao Portal." /></p><hr />
<FORM METHOD="POST" ACTION="cadastro_validar.php">
<div class="mensagemErro"></div>
<table width="230px" height="100px" align="center" border="0" class="BoxLogin">
<tr>
<td colspan="2" align="center" class="textoIndex"><b>Primeiro Cadastro</b><br /><p align="left">» Insira seu CPF no campo abaixo.</p></td>
</tr>
<tr>
<td class="textoIndex">CPF:</td>
<td><input name="cpf" id="cpf" onblur="javascript: validarCPF(this.value);" onkeypress="javascript:mascara(this, cpf_mask);" maxlength="14" type="text" class="estiloBotao" size="40" /></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" name="enviar" value="Validar" bgcolor="#E0E0E0" class="confirma" /></td>
</tr>
</table>
</FORM><br />
<div id="cadastrarMenu">
<table width="200px" height="50px" align="right" border="0" class="BoxLogin">
<tr class="BoxLogin">
<td align="center" class="textoIndex"><img src="imagens/cadastrarMembro.png" width="27" height="29"align="center" />Faça login</td>
</tr>
<tr>
<td align="center"><a href="index.php">
<input type="button" bgcolor="#E0E0E0" value="Logar no sistema" class="confirma" /></a></td>
</tr>
</table>
</div>
<div id="esqueciSenha">
<table width="210px" height="50px" align="left" border="0" class="BoxLogin">
<tr class="BoxLogin">
<td align="center" class="textoIndex"><img src="imagens/cadastrarMembro.png" width="27" height="29"align="center" />Esqueceu a Senha?</td>
</tr>
<tr>
<td align="center"><a href="recuperar.php">
<input type="button" value="Recuperar" bgcolor="#E0E0E0" class="confirma" /></a></td>
</tr>
</table>
</div>
<br />
<br /><br /><p align="center"> </p>
<div id="footer">
<p align="center">© 2014 - Ministério Glória de Cristo</p>
</div>
</body>
</html>
// CADASTRO VALIDAR
*******************************************************************
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sistema de Gestão Eclesiástico</title>
<style type="text/css">
.textoIndex {
font-family: "Lucida Console", Monaco, monospace;
color: #333;
font-size: 16px;
}
.BoxLogin {
border:1px solid #999;
moz-border-radius:10px;
-webkit-border-radius:10px;}
.estiloBotao {
-webkit-transition: all 0.30s ease-in-out;
-moz-transition: all 0.30s ease-in-out;
-ms-transition: all 0.30s ease-in-out;
-o-transition: all 0.30s ease-in-out;
outline: none;
padding: 3px 0px 3px 3px;
margin: 5px 1px 3px 0px;
border: 1px solid #DDDDDD;
}
.estiloBotao:focus {
box-shadow: 0 0 5px rgba(81, 203, 238, 1);
padding: 3px 0px 3px 3px;
margin: 5px 1px 3px 0px;
border: 1px solid rgba(81, 203, 238, 1);
}
.confirma {
padding:5px 15px; background:#ccc; border:0 none;
cursor:pointer;
-webkit-border-radius: 5px;
border-radius: 5px;
}
.confirma:hover {
background:#0CF;
}
#cadastrarMenu {
float:left;
width: 50%;
}
#esqueciSenha {
float:right;
width: 50%;
}
.mensagemErro {
border: 1px solid;
margin: 10px 0px;
padding:15px 10px 15px 50px;
background-repeat: no-repeat;
background-position: 10px center;
}
.mensagemErro {
color: #D8000C;
background-color: #FFBABA;
background-image: url('imagens/erro.png');
}
#footer {
position: absolute;
bottom: 0px;
width: 100%;
height: 60px;
background-color: #CCC;
left: 1px;
}
</style>
</head>
<body>
<?php
include "conexao.php";
$cpf = $_POST['cpf'];
print $cpf;
$query_CPF = "SELECT CPF FROM dados_usuarios WHERE cpf = '".$cpf."' ";
$resultado_CPF = mysql_query($query_CPF, $conexao) or die (mysql_error());
$linhas_CPF = mysql_fetch_array($resultado_CPF) or die (mysql_error());;
$contador_CPF = mysql_num_rows($resultado_CPF);
print $contador_CPF;
if ($contador_CPF == 1) {
print "Usuário já cadastrado no sistema";print "Usuário não existe no sistema";
}
?>
<p align="center"><img src="imagens/logo.jpg" alt="Bem-vindo ao Portal." /></p><hr />
<div class="mensagemErro"></div>
<br />
<FORM METHOD="POST" ACTION="cadastro_validar.php">
<table width="230px" height="100px" align="center" border="0" class="BoxLogin">
<tr>
<td colspan="2" align="center" class="textoIndex"><b>Primeiro Cadastro</b><br /><p align="left">» Insira seu CPF no campo abaixo.</p></td>
</tr>
<tr>
<td class="textoIndex">CPF:</td>
<td><input name="cpf" id="cpf" onblur="javascript: validarCPF(this.value);" onkeypress="javascript:mascara(this, cpf_mask);" maxlength="14" type="text" class="estiloBotao" size="40" /></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" name="enviar" value="Validar" bgcolor="#E0E0E0" class="confirma" /></td>
</tr>
</table>
</FORM><br />
<div id="cadastrarMenu">
<table width="200px" height="50px" align="right" border="0" class="BoxLogin">
<tr class="BoxLogin">
<td align="center" class="textoIndex"><img src="imagens/cadastrarMembro.png" width="27" height="29"align="center" />Faça login</td>
</tr>
<tr>
<td align="center"><a href="index.php">
<input type="button" bgcolor="#E0E0E0" value="Logar no sistema" class="confirma" /></a></td>
</tr>
</table>
</div>
<div id="esqueciSenha">
<table width="210px" height="50px" align="left" border="0" class="BoxLogin">
<tr class="BoxLogin">
<td align="center" class="textoIndex"><img src="imagens/cadastrarMembro.png" width="27" height="29"align="center" />Esqueceu a Senha?</td>
</tr>
<tr>
<td align="center"><a href="recuperar.php">
<input type="button" value="Recuperar" bgcolor="#E0E0E0" class="confirma" /></a></td>
</tr>
</table>
</div>
<br />
<br /><br /><p align="center"> </p>
<div id="footer">
<p align="center">© 2014 - Ministério Glória de Cristo</p>
</div>
</body>
</html>Carregando comentários...