Registration form

0

I'm making a registration form and I do not know how to proceed in this case:

The form has mandatory fields, if the user clicks "ok" without one of these fields being filled, returns a message asking the user to fill out the form. So far so good. But after returning to the form, the fields are blank again, I would like it when I returned with the error message, the data that the user had previously filled in remained

    
asked by anonymous 08.02.2018 / 19:56

2 answers

0

User JAVASCRIPT, I'll use an example form. I've made a more simplified method using a single BOLEAN variable to tell if it has some empty field or not. Making it TRUE (valid) by default and a condition (IF) for each required field that will make it FALSE invalid if empty or null.

function validarFormulario(){
	var validado = true;
	var meuform = document.forms['meuformulario'] || document.meuformulario;
	/* Checa cada campo obrigatorio
	* Insira uma IF referente somente a cada campo obrigatorio
	*/
	if(meuform.nome.value == "" || meuform.nome.value == null){
		validado = false;
	}
	if(meuform.telefone.value == "" || meuform.telefone.value == null){
		validado = false;
	}

	/* informa se qualquer estiver errado ou envia o formulario */
	if(validado == false){
		alert("Preencha os campos obrigatórios");
	}else{
		meuform.submit();
	}
}
<form action="#" method="POST" name="meuformulario">
<p>* Nome:<input type="text" value=""  name="nome" /></p>
<p>* Telefone:<input type="text" value=""  name="telefone" /></p>
<p>Apelido:<input type="text" value=""  name="apelido" /></p>
<p><input type="button" value="enviar" onclick="validarFormulario()" /></p>
</form>
<p>Campos com "*" são obrigatórios.</p>

Now if you really want PHP to return pre-populated values after validating and the error due to lack of padding, simply insert with the Global variables of the POST method the values of the fields:

* Nome:<input type="text" value="<?php echo $_POST['nome']?>"  name="nome" />

But this is a simple method of thing, I would advise doing self-completion with JScript in conjunction with PHP to keep organized.

    
08.02.2018 / 23:45
0

Html5 has a property for components called required. This makes field filling mandatory. You can see more of this at this link: link

    
09.02.2018 / 19:48