-1
I have a script that reads each field of the form:
// JavaScript Document
$(document).ready(function (e) {
$("#cadastrar").on("click", function () {
$(".required").each(function (i) {
if ($(this).val() == "") {
idCampo = $(this).attr("id");
nomeCampo = idCampo.substr(0, 1).toUpperCase() + idCampo.substr(1);
alert("Preecha campo " + nomeCampo + ", campo obrigatório");
$(this).focus();
return false;
};
});
});
And I have a date field in the form:
<input type="date" max="<?php echo date('Y-m-d'); ?>" class="inputTextMedio required" name="nascimento" id="nascimento" required/>
I’m having 2 problems here.
In the loop foreach
, when it arrives in the false Return it up to.
But even so it seems that it continues because I have below a ajax
which can only be called if the validations occur normally. But is passing directly.
foreach.....
$.ajax({
url: "_scripts/_php/_validacoes/cadastrarMembro.php",
type: "POST",
dataType: "json",
data: {
dataCadastro : $("#dataCadastro").val(),
nome : $("#nome").val(),
apelido : $("#apelido").val(),
nascimento : $("#nascimento").val(),
telefone : $("#telefone").val(),
celular : $("#celular").val(),
bairro : $("#bairro").val(),
endereco : $("#endereco").val(),
email : $("#email").val(),
sexo : $("#sexo").val(),
estadoCivil : $("#estadoCivil").val(),
dataBatismo : $("#dataBatismo").val(),
bloqueado : $("#bloqueado").val(),
batizadoFora : $("#batizadoFora").val(),
usuario : $("#usuario").val(),
senha : $("#senha").val()
},
success: function (result) {
if (result[0] == "Erro")
$(".resposta").html(result[1]);
else
$(".resposta").html(result[1]);
}
});
});
});
Where am I going wrong?
complete code:
// JavaScript Document
$(document).ready(function (e) {
$("#cadastrar").on("click", function () {
$(".required").each(function (i) {
if ($(this).val() == "") {
idCampo = $(this).attr("id");
nomeCampo = idCampo.substr(0, 1).toUpperCase() + idCampo.substr(1);
alert("Preecha campo " + nomeCampo + ", campo obrigatório");
$(this).focus();
break();
};
});
$.ajax({
url: "_scripts/_php/_validacoes/cadastrarMembro.php",
type: "POST",
dataType: "json",
data: {
dataCadastro : $("#dataCadastro").val(),
nome : $("#nome").val(),
apelido : $("#apelido").val(),
nascimento : $("#nascimento").val(),
telefone : $("#telefone").val(),
celular : $("#celular").val(),
bairro : $("#bairro").val(),
endereco : $("#endereco").val(),
email : $("#email").val(),
sexo : $("#sexo").val(),
estadoCivil : $("#estadoCivil").val(),
dataBatismo : $("#dataBatismo").val(),
bloqueado : $("#bloqueado").val(),
batizadoFora : $("#batizadoFora").val(),
usuario : $("#usuario").val(),
senha : $("#senha").val()
},
success: function (result) {
if (result[0] == "Erro")
$(".resposta").html(result[1]);
else
$(".resposta").html(result[1]);
}
});
});
});
I also tried the way below and nothing.
requeridos = $(".required");
eachConta = requeridos.length;
alert(eachConta);
for (i=0; i< eachConta; i++) {
if (requeridos[i].val() == "") {
idCampo = requeridos[i].attr("id");
nomeCampo = idCampo.substr(0, 1).toUpperCase() + idCampo.substr(1);
alert("Preecha campo " + nomeCampo + ", campo obrigatório");
requeridos[i].focus();
return false;
};
}
shame. kkk. Thank you! tiredness does things
– Carlos Rocha
but, and the problem of the date field?
– Carlos Rocha
What would be the problem with the countryside?
– Sam
then, the field is this <input type="date" max="<? php echo date('Y-m-d'); ? >" class="inputTextMedio" name="dataBatismo" id="dataBatismo"/>. If I choose a date all right. But if I do not choose from the error in mysql query and mysql the field accepts empty or null
– Carlos Rocha
in jquery ajax, before sending to php, I did so $_POST["dataBatismo"] == ""? 'NULL' : $_POST["dataBatismo"] ,, since the field accepts null value
– Carlos Rocha
Try to use
empty($_POST["dataBatismo"]) ? 'NULL' : $_POST["dataBatismo"]
– Sam
same thing. We used to open the imspector of browsers and we could see the return of php to ajax. Now sometimes even the right javascript are displaying on the ispector
– Carlos Rocha