Mandatory completion

Asked

Viewed 1,395 times

0

<label for=""><h5><strong>Dia da Semana</strong></h5></label>
<select name="Dia">
       <option value="0">Selecione o Dia</option>
        <?php
         $servername = "xxx.xxx.xx.xx";
$username = "xxxx";
$password = "xxxxxxx";
$dbname = "xxxxx";

$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset('utf8'); 

         $sql = "SELECT * FROM centrodb.DiasSemana ORDER BY Dias ASC";
         $qr = mysqli_query($conn, $sql);
         while($ln = mysqli_fetch_assoc($qr)){
            echo '<option value="'.$ln['Dias'].'">'.$ln['Dias'].'</option>';
         }
      ?>        
    </select>

I want this form field to be mandatory

3 answers

1


Make the logic that if it is empty or does not contain the expected data, show the message asking to fill in.

If it’s Html5 you can use

<input type="text" name="nome_do_campo" required>
  • But in this case it’s not a input, is <select name="Dia"></select> and put this and does not require the filling

  • select also has the attribute required

1

From what I understand this your code you meant required to be completed by the user and not by PHP. So let’s understand the dynamically generated HTML options, even if they were static values.

You can check the option selected as ARRAY of the SELECT using the selectedIndex to identify the selected option.

Then the Selecione o Dia is a populated value, so you will need to validate with JAVASCRIPT:

function validarFormulario(){
	var myform = document.forms['formulario'] || document.formulario;
	if(myform.Dia.value == "0" || myform.Dia.selectedIndex == 0){
		alert('Preencha a data');
	}else{
		myform.submit();
	}
}
<form name="formulario" action="arquivo.php" method="POST">
<select name="Dia">
  <option value="0">Selecione...</option>
  <option value="2015">2015</option>
  <option value="2016">2016</option>
  <option value="2017">2017</option>
  <option value="2018">2018</option>
</select>
<input type="button" value="Enviar" onclick="validarFormulario()" />
</form>

You can also validate with PHP:

if($_SERVER['method'] === "POST"){
   if($_POST['Dia'] == "" || $_POST['Dia'] == null){
      /* Não Preenchido */
   }else{
      /* Preenchido */
   }
}
  • In the first option actually appears the message, but when doing ok it inserts the row in the same to be populated in the table of the database

  • It was my mistake, I wrote the IF instead of how it should work, it is worth remembering that you should insert the entire method of recording the data within the ELSE. I improved the form explanation and fixed an error of the PHP function

0

If you want to use only php, recommend so

After the tag <select>, an error message should appear saying you are wrong.

Use this code

<?php 
if(isset($_POST['botao_submit']) && $_POST['Dia'] != "")){
echo "Campo Obrigatório";
}
?>

With the above code it would check if the form had been submitted, if it had been submitted and the field had been empty, it would appear that code.

Use the required in the select also works but if someone were to Inspecionar Elemnto could well eliminate the required select and send blank, with that code not.

  • And within the if query to the database?

  • I edited the answer

Browser other questions tagged

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