Registration form

Asked

Viewed 124 times

0

I’m making a registration form and I don’t 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. Only that after returning to the form, the fields are blank again, I would like that when returned with the error message, the data that the user had previously filled in continued

  • 1

    Post your code to facilitate the solution.

  • 1

    Explain better how you are doing. When filling the form, the user is submitted to another page and returns or it is all in one page?

  • Why aren’t you using javascript to check before sending the form?

2 answers

0

0


User JAVASCRIPT, I will use an example form. I made a more simplified method, using a single BOLEAN variable to tell if there is an 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 by fulfilling the previous values after validating and the error due to lack of completion just 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 the thing, I would advise doing the self-prefilling with Jscript in conjunction with PHP to keep organized.

Browser other questions tagged

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