2
I have a code that I need that when submitted, some fields are mandatory to be filled in, type o required
html only in Ajax, instead of using Alert as I am using, it uses required
as in html. Follow my code below:
function d(func){
var nome = $('#nome').val();
var data = $('#data_oco').val();
var tipo = $('#tipo').val();
var justificativa = $('#justificativa').val();
if(justificativa==undefined){
justificativa="";
}else if(justificativa=="" && tipo!="Falta"){
alert("Por favor preencha o campo Justificativa!");
}
jQuery.ajax({
method: "get",
url: "atudel.php",
data: { "nome": nome, "data": data, "tipo": tipo, "justificativa": justificativa, "func": func },
success: function(data){
alert(data);
window.location = 'editar_ocorrencia.php';
}
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="cabesalho_formulario">
<h1 id="titulo">Controle Geral de Alunos <span>(Edição)</span></h1>
<h4 id="nome_escola">EEEP Dr. José Alves da Silveira</h4>
<div id="atualiza" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Atualizar/Deletar Ocorrência</h4>
</div>
<div class="modal-body">
<br class='b'><label id='just'>Justificativa: </label><br class='b'><textarea required placeholder='Digite a justificativa do aluno...' title='Seja objetivo(a).' rows='5' name = 'obs' cols='50' id='justificativa'></textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" onclick="d('atualiza')">Atualizar</button>
<button type="button" class="btn btn-default" data-dismiss="modal" onclick="d('deleta')">Deletar</button>
</div>
</div>
</div>
</div>
Instead, it looks like this, only in a way that uses jquery inside the js function:
<form method="" action="#">
<div id="cabesalho_formulario">
<h1 id="titulo">Controle Geral de Alunos <span>(Edição)</span></h1>
<h4 id="nome_escola">EEEP Dr. José Alves da Silveira</h4>
<div id="atualiza" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Atualizar/Deletar Ocorrência</h4>
</div>
<div class="modal-body">
<br class='b'><label id='just'>Justificativa: </label><br class='b'><textarea required placeholder='Digite a justificativa do aluno...' title='Seja objetivo(a).' rows='5' name = 'obs' cols='50' id='justificativa'></textarea>
</div>
<div class="modal-footer">
<input type="submit" class="btn btn-default" data-dismiss="modal" value="Atualizar">
<input type="submit" class="btn btn-default" data-dismiss="modal" value="Deletar">
</div>
</div>
</div>
</div>
</form>
Okay, but if I hit several times the same button the message appears several times.
– Carlos Henrique
@Carloshenrique easy to solve. I will update.
– Sam
@Carloshenrique I just added an IF. Abs!
– Sam
Cool. Simple and practical solution ;)
– Giovanni Mesquita