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.