strange validation error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cmdo
Forum Newbie
Posts: 13
Joined: Wed Nov 30, 2011 2:04 pm

strange validation error

Post by cmdo »

Hi all,
I have a small question here.
I have this piece of code to validate if the fields are empty or not

Code: Select all

if(!$name)
        {
            $err[] = "Deverá indicar o seu nome!";
        }
	if(!$email)
        {
            $err[] = "Deverá indicar o seu email!";
        }
        if(!$mensagem)
        {
            $err[] = "Deverá escrever a sua mensagem!";
        }
But the php keeps showing me that the fields are empty :s I don't understand why.
Can you guys help me here?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: strange validation error

Post by Celauran »

What does var_dump tell you?

Code: Select all

var_dump($name);
User avatar
Tiancris
Forum Commoner
Posts: 39
Joined: Sun Jan 08, 2012 9:54 pm
Location: Mar del Plata, Argentina

Re: strange validation error

Post by Tiancris »

Validation seems correct, but what is the origin of these values? Arrays like $_GET or $_POST? A result from database query?
You should check the code where variables are loaded.
cmdo
Forum Newbie
Posts: 13
Joined: Wed Nov 30, 2011 2:04 pm

Re: strange validation error

Post by cmdo »

var_dump gives me NULL

I'm getting the values from textfield's and the form method is POST
I don't understand this :s I have the same code working in a different page.
User avatar
Tiancris
Forum Commoner
Posts: 39
Joined: Sun Jan 08, 2012 9:54 pm
Location: Mar del Plata, Argentina

Re: strange validation error

Post by Tiancris »

Try var_dump($_POST).
Can you see there the values sent from the form?
Are you sure you are copying (for example) $_POST ['name'] in $name?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: strange validation error

Post by social_experiment »

cmdo wrote:But the php keeps showing me that the fields are empty :s I don't understand why.
What code is before the sample you pasted; For an empty check i would suggest something like

Code: Select all

<?php 
if ($name == '') $err[] = 'Empty name field'; 
// or
if (empty($name))  $err[] = 'Empty name field'; 
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
cmdo
Forum Newbie
Posts: 13
Joined: Wed Nov 30, 2011 2:04 pm

Re: strange validation error

Post by cmdo »

It gives me this:
array(0) { }

This is my php code:
session_start();
if (isset($_GET['enviar']))
{
$err = array();
$suc = array();
$name = $_POST['name'];
$email = $_POST['email'];
$mensagem = $_POST['mensagem'];
var_dump($_POST);
if(!$name)
{
$err[] = "Deverá indicar o seu nome!";
}
if(!$email)
{
$err[] = "Deverá indicar o seu email!";
}
if(!$mensagem)
{
$err[] = "Deverá escrever a sua mensagem!";
}

if(!count($err))
{
//define the receiver of the email
$to = 'makemilkdesign@gmail.com';
//define the subject of the email
$subject = 'Formulário de Contacto';
//define the message to be sent. Each line should be separated with \n
$message = $mat."\n".$obra."\n".$func."\n".$saidas;
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: Registo de Saída de Material material@rafael.13studiohost.com \r\nReply-To: Registo de Saída de Material material@rafael.13studiohost.com";
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
if (!$mail_sent)
{
$err[] = "E-mail não enviado!";
}else{
$suc[] = "E-mail enviado com sucesso!";
}
}
if($err)
{
$_SESSION['msg']['err'] = implode('<br />',$err);
}
if($suc)
{
$_SESSION['msg']['suc'] = implode('<br />',$suc);
}
header("Location: index.php");
exit;
}
?>

and this is my html
<div id="content">
<img src="imagens/makemilk.png">
<h1>Let's make some milk!</h1>
<div id="introd_top">&nbsp;</div>
<div id="introd"><div id="texto"><h2>Fazemos os seus websites a preços competitivos.<br>Contacte-nos já para obter mais informações!</h2></div></div>
<div id="introd_bot">&nbsp;</div>
<div>&nbsp<br></div>
<?php
if(isset($_SESSION['msg']['err']))
{
echo '<div class="err">'.$_SESSION['msg']['err'].'</div>';
unset($_SESSION['msg']['err']);
}
?>
<?php
if(isset($_SESSION['msg']['suc']))
{
echo '<div class="suc">'.$_SESSION['msg']['suc'].'</div>';
unset($_SESSION['msg']['suc']);
}
?>
<form action="#" method="post">
<div id="contacto">
<label for="name">Insira aqui o seu nome...</label>
<input type="text" name="name" id="name">
<label for="email">Insira aqui o seu email...</label>
<input type="text" name="email" id="email">
<label for="mensagem">Insira aqui a sua mensagem...</label>
<textarea id="mensagem" name="mensagem"></textarea>
</div>
<div id="enviar"><a href="?enviar"><img src="imagens/enviar.png"></a></div>
</form>
</div>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: strange validation error

Post by social_experiment »

Code: Select all

<?php
$err = array();
$suc = array();
$name = $_POST['name'];
$email = $_POST['email'];
$mensagem = $_POST['mensagem'];
var_dump($_POST);
?>
At this point, unless you submitted the form, $_POST contains no information. You could try checking that the button was clicked before assigning the $_POST variables
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
cmdo
Forum Newbie
Posts: 13
Joined: Wed Nov 30, 2011 2:04 pm

Re: strange validation error

Post by cmdo »

What do you mean?
I'm sorry but I do not understand :S
cmdo
Forum Newbie
Posts: 13
Joined: Wed Nov 30, 2011 2:04 pm

Re: strange validation error

Post by cmdo »

I got it working
I changed it from picture to button with a background picture.
Thanks for the help.
Post Reply