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.
Post your code to facilitate the solution.
– Valdeir Psr
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?
– Sam
Why aren’t you using javascript to check before sending the form?
– Kyle A