2
I created a radio form, which when filling the alternatives, when clicking on the Ubmit button, it sends the form data via AJAX to the PHP page applying the logic of how many questions were hit. The clicked button generates a div per popup with the return of PHP logic, showing how many hits were made. But by clicking the button even without filling all radios, it generates the empty div on the screen. All fields are with required
.
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('#questionario').submit(function(){
var dados = jQuery( this ).serialize();
jQuery.ajax({
type: "POST",
url: "teste.php",
data: dados,
success: function( data )
{
$("#div1").html(data);
var modal1= document.getElementById('modal-wrapper1');
window.onclick= function(event){
if(event.target==modal1){
modal1.style.display="none";
}
}
}
});
return false;
});
});
</script>
<section class="joguinho" style="background:#f2f2f2;">
<div class="container">
<div class="col-md-12 justify-content-center d-flex">
<h1 style="font-weight: 300;" class="mt-5">DESCUBRA</h1>
</div>
<div class="col-md-12 justify-content-center d-flex">
<h1 style="color: #03be98;">SE VOCÊ TEM DTM</h1>
</div>
<form action="" method="post" id="questionario">
<div class="row">
<div class="col-12 col-md-9"><h6>Sente dificuldade para abrir a boca?</h6></div>
<div class="col-12 col-md-3 form-check text-left text-md-right">
<input type="radio" name="questao1" id="questao1-1" value="Sim" required> Sim
<input type="radio" name="questao1" id="questao1-2" value="Não" required> Não
</div>
</div> <br>
<div class="row">
<div class="col-12 col-md-9"><h6>Sente dificuldade para movimentar a mandíbula para os lados?</h6></div>
<div class="col-12 col-md-3 form-check text-left text-md-right">
<input type="radio" name="questao2" id="questao2-1" value="Sim" required> Sim
<input type="radio" name="questao2" id="questao2-2" value="Não" required> Não
</div>
</div> <br>
<button type="submit" value="Enviar" style="background:#690d2e; color:white; font-weight: 600" class="btn" onsubmit="document.getElementById('modal-wrapper1').style.display='block'">GERAR RESULTADO</button>
</form>
</div>
<!--Formulario invisivel-->
<div id="modal-wrapper1" class="modal">
<div class="container justify-content-center d-flex align-self-center modal-content animate">
<div id="div1" class=" justify-content-center d-flex align-self-center form-row pt-2">
</div>
</div>
</div>
</section>
your question is not very clear about the problem, but want to send only with the fields filled, it would be interesting to validate the form before, for example using the jquery Valid() function
– Ricardo Pontual
One option is to disable the Submit button with
disabled
and only enable after filling the radio Buttons. It would solve so?– gustavox