Radio de Preenchimento Obrigatório html

Asked

Viewed 2,217 times

3

How do I place this mandatory field:

<p><h5><strong>Estado</strong></h5>
 <p>&nbsp;&nbsp;&nbsp; <input type="radio" name="Estado" value="Concluído">Concluído 
  • You should not only add the required input, but also do a validation in php for greater security. If you put what code you receive this post or get after sending the form. Maybe we can help you better.

2 answers

1


Just add the required attribute to the input tag. It would look something like:

<input type="radio" name="Estado" value="Concluído" required>Concluído
  • The safest is to do a validation in php itself. As the user can by Debugger simply delete the required.

  • Yes, it is a security issue to validate all fields both on the front and back. Validating the data on the front serves mainly for a better user experience, which will already know what filled wrong before sending the form.

0

You can do a check front-end with Javascript (the e.preventDefault(); will prevent the form from being sent):

document.querySelector("form").addEventListener("submit", (e)=>{
   if(!document.querySelector("input[name='Estado']").checked){
      alert("Marque o radio button");
      e.preventDefault();
   }
});

And do the check on back-end in PHP (most importantly!):

<?php
$Estado = $_POST['Estado'];

if(isset($Estado) && !empty($Estado)){
   echo "Estado foi checado";
}else{
   echo "Estado não foi checado";
}
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.