1
I created a client registration method with Ajax via Post. But somehow my code should contain some error when I establish a certain condition. Note below: when the value returned from registering.php is "Registered", I would like it to open a certain Bootstrap modal window with the information that the client was successfully registered. It turns out that in the case below, he must not be correctly recognizing the expression, as he is always jumping to "I" and opening the wrong window. Could you tell me if my "if" condition contains an error?
// Envio dos dados cadastrais do cliente
jQuery(document).ready(function(){
jQuery('#cadastrarcliente').submit(function(){
var dados = jQuery( this ).serialize();
jQuery.ajax({
type: "POST",
url: "cadastrar.php",
data: dados,
success: function(data) {
if (data == "Cadastrado")
$('#enviocliente').modal();
else $('#errologinduplicado').modal();
}
});
return false;
});
});
And the PHP code that returns the message "Registered"
<?php ob_start();
include("config.php"); //Arquivo configuração do BD
//Captura todos os dados informados em login.php
$telefixo = $_POST['telefixo'];
$nome = utf8_decode($_POST['nome']);
$rg = utf8_decode($_POST['rg']);
$cpf = utf8_decode($_POST['cpf']);
$usuario = $_POST['usuario'];
$senha = $_POST['senha'];
$email = $_POST['email'];
$sexo = $_POST['sexo'];
$celular = $_POST['celular'];
$cep = $_POST['cep'];
$residencia = $_POST['residencia'];
$endereco = utf8_decode($_POST['endereco']);
$numero = $_POST['numero'];
$cidade = utf8_decode($_POST['cidade']);
$uf = $_POST['uf'];
$pessoa = utf8_decode($_POST['pessoa']);
$datanasc = $_POST['data'];
$estadocivil = $_POST['estadocivil'];
$profissao = utf8_decode($_POST['profissao']);
$foto = 'anonymous.jpg';
$sql_busca = mysqli_query($bd,"SELECT * FROM user WHERE cpf='$cpf'");
$num_busca = mysqli_num_rows($sql_busca);
// Verifica se já não existe uma credencial com mesmo CPF na tabela user
if ($num_busca == 0) {
//Se não existir, grava os dados informados na tabela user
$sql_inclu = "INSERT INTO user (nome, usuario, senha, email, sexo, celular, cep, residencia, endereco, numero, cidade, uf, estadocivil, profissao, pessoa, datanasc, foto, rg, cpf, telefixo) VALUES ('$nome', '$usuario', '$senha', '$email', '$sexo', '$celular', '$cep', '$residencia', '$endereco', '$numero', '$cidade', '$uf', '$estadocivil', '$profissao', '$pessoa', '$datanasc', '$foto', '$rg', '$cpf', '$telefixo')";
$exe_inclu = mysqli_query ($bd, $sql_inclu);
echo "Cadastrado";
}
else {
echo "Erro";
}
?>
Alan, put an Alert(date); before the if and see what is being returned.
– Givanildo R. de Oliveira
If you have returned Registered, you probably have some space. Then use it as this condition: if( $.Trim(date) == "Registered").
– Givanildo R. de Oliveira
I made the Alert and it is returning me Registered same. I tried the expression you gave me and nothing happens. I do not know where can be the error.
– Alan Garmatter
There’s a way you can provide a little bit of the registration code.php that returns this?
– Givanildo R. de Oliveira
Hi Givanildo. Yes, I just updated the post. Take a look, please.
– Alan Garmatter
Dude, I took your code and it worked fine for me. I honestly don’t know what it could be. I put this console.log('>'+data+'<'); before the if just to see if it was returning without space or some character, and all right here.
– Givanildo R. de Oliveira
So, as I understand it, the problem is that the expression date == 'Registered' is not being interpreted here in my case. I was able to solve using numerals, because since it does not use quotation marks and javascript can recognize directly, the expression performs perfect. Ex: If (date == 1) { ... Anyway, thanks for the help.
– Alan Garmatter