0
When making a registration using ajax, Success does not execute the commands from within it, when analyzing the code I noticed that there is a /ufeff in the value returned by php and I think this is causing the error.
Php code:
<?php
header("Content-Type: text/html; charset=UTF-8",true);
require "Conexao.php";
if (verificacaocCliente() == true) {
$nome = mysqli_real_escape_string($conexao, $_POST["nome"]);
$cpf = mysqli_real_escape_string($conexao, $_POST["cpf"]);
$rg = mysqli_real_escape_string($conexao, $_POST["rg"]);
$telefone = mysqli_real_escape_string($conexao, $_POST["telefone"]);
$celular = mysqli_real_escape_string($conexao, $_POST["celular"]);
$endereco = mysqli_real_escape_string($conexao, $_POST["endereco"]);
$bairro = mysqli_real_escape_string($conexao, $_POST["bairro"]);
$uf = mysqli_real_escape_string($conexao, $_POST["uf"]);
$cidade = mysqli_real_escape_string($conexao, $_POST["cidade"]);
$date_nasc = mysqli_real_escape_string($conexao, $_POST["date_nasc"]);
$email = mysqli_real_escape_string($conexao, $_POST["email"]);
$data = date("Y/m/d");
$sql = "INSERT INTO cliente (Nome, CPF, RG, Telefone, Celular, Endereco, Bairro, UF, Cidade, Data_Nascimento, Email, Destaque, Negativo, Data_Insercao) VALUES ('$nome', '$cpf', '$rg', '$telefone', '$celular', '$endereco', '$bairro', '$uf', '$cidade', '$date_nasc', '$email', 0, 0, '$data')";
$result = mysqli_query($conexao, $sql) or die (mysqli_error($conexao));
if ($result) {
$response = array("success" => true);
echo json_encode($response);
}
}
Ajax:
$.ajax({
url: "Scriptsphp/novoCliente.php",
method: "POST",
dataType: "json",
data: { nome: nome, cpf: cpf, rg: rg, telefone: telefone, celular: celular, endereco: endereco, bairro: bairro, uf: uf, cidade: cidade, date_nasc: date_nasc, email: email },
cache: false,
beforeSend: function() {
$("#sbmt_cCliente").val("Cadastrando...");
},
success: function(response) {
if (response) {
$("#erro_cCliente").slideDown("fast").html("Cliente cadastrado !");
$("#nome, #cpf, #rg, #telefone, #celular, #endereco, #bairro, #email").val("").css({ "border": "1px solid #7D96BF" });
$("#uf, #cidade").val("Selecione...").css({ "border": "1px solid #7D96BF" });
$("#date_nasc").val("dd/mm/aaaa").css({ "border": "1px solid #7D96BF" });
} else {
$("#erro_cCliente").slideDown("fast").html("Erro no cadastro !");
$("#sbmt_cCliente").val("Cadastrar");
}
}
how can I remove /ufeff ?
Have you ever tried to change the
if(response)
forif(response.success)
?– KillerJack
Before the
json_encode
in PHP, puthttp_response_code(200)
.– Woss
Not yet, I tested the two tips but it’s still the same, the data is entered in the database but not executed what is inside the Success
– Daniel Santos
What version of Jquery are you using?
– KillerJack
I am using jquery 3.2.0
– Daniel Santos
Try to change
success: function(response)
fordone: function(response)
then.– KillerJack
Not yet, the tin is that it was working this way a little while ago, and I made small changes in php like $data = date("Y/m/d"), and after these modifications it stopped working the Success but still works inserting in the bank @Killerjack
– Daniel Santos
Really weird. Add after the
beforeSend
:always: function(obj, msg) { console.log(msg); },
and see in devtools or equivalent what appears.– KillerJack
Sorry for the mistake, but I’ve never used aways before, and while using it now I realize that nothing appears on the console, probably because I did something wrong, however, I notice something that may be the problem of all this, when I give the
echo json_encode($response);
there is a\ufeff
before the{"success":true}
(thing that does not happen in any other echo), it could be that ?– Daniel Santos
Update your question with the current code. Which editor are you using? Try switching the file’s Encounter to something without GOOD.
– KillerJack
I’m using vscode, and I’ve made a pattern of Ncode across the site, reworked the question with the code
– Daniel Santos
Change your
Content-Type
forapplication/json
.– Woss
continues with /ufeff
– Daniel Santos
Did you change the file encoding in your editor? Vscode has the option to choose encoding in some corner, I haven’t used it for a while so I can’t tell you exactly where. If it has with GOOD there, it will always put when saving the file.
– KillerJack
The problem was really the GOOD, however I was looking in the wrong place, the problem was not in this file, but in the
conexao.php
, thanks for your patience and help @Killerjack and @Anderson Carlos Woss– Daniel Santos