1
I am validating a form where the user will fill in the fields :
- Name
- Surname
- Telephone
- Message
Through these, I want to validate if the user does not type in these fields appears a message saying that it is necessary to fill in the fields.
I’ve managed to create in php
a validation for these fields, but as I am a programmer still new in php
,I don’t know if I’m doing this call right php
,within the html
I’m using method post
,to pick up the fields.
Php code :
<html>
<body>
<?php
$nome = $_POST["nome"];
$sobrenome = $_POST["sobrenome"];
$email = $_POST["email"];
$telefone = $_POST["telefone"];
$mensagem = $_POST["mensagem"];
$erro = 0;
//Verifica se o campo nome não está em branco.
if(empty($nome) OR strstr ($nome,'')==false)
{
echo("Favor digitar seu nome"; $erro =1;);
}
//Verifica se o campo sobrenome não está em branco.
if(empty($sobrenome) OR strstr ($nome,'') ==false)
{
echo("Favor digitar seu sobrenome";$erro=1;);
}
//Verifica se o campo email não está em branco.
if(strlen($email)<8 || strstr($email,'@')==false)
{
echo("Favor digitar seu email corretamente";$erro = 1;);
}
//Verifica se o campo telefone está sendo preenchido com texto.
if(!is_numeric($telefone))
{
echo("Preencha o campo telefone somente com números.";$erro = 1;);
}
//Verifica se o campo email não está em branco.
if(empty($mensagem) OR strstr ($mensagem,'') ==false)
{
echo("Favor digitar sua mensagem";erro = 1;)
}
?>
</body>
</html>
I’m using the form action = "validar.php"
in the form. At the time I place to send the form, it appears nothing, php
in which I created, called validar.php
.Doesn’t even check the fields.
How can I make this work ? If anyone can help me I will be grateful.
When you submit the form it goes directly to action, and that file is where you have to do the validations
– Miguel
Why not use the required tag in form inputs?
– Diego
This code you have placed is from the archive
validar.php
? Also put your html code.– abfurlan
@Diego I am now using the required tag on the form inputs.
– Falion
@Falion that solves your problem right?
– Diego
No, actually almost, because I want you to have specific messages for each textbox, for example the name : "Fill in the field with your name".
– Falion