Page 1 of 1

strange validation error

Posted: Mon Jan 16, 2012 1:14 pm
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?

Re: strange validation error

Posted: Mon Jan 16, 2012 2:03 pm
by Celauran
What does var_dump tell you?

Code: Select all

var_dump($name);

Re: strange validation error

Posted: Mon Jan 16, 2012 2:17 pm
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.

Re: strange validation error

Posted: Mon Jan 16, 2012 3:13 pm
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.

Re: strange validation error

Posted: Mon Jan 16, 2012 3:22 pm
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?

Re: strange validation error

Posted: Mon Jan 16, 2012 3:24 pm
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'; 
?>

Re: strange validation error

Posted: Mon Jan 16, 2012 3:25 pm
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>

Re: strange validation error

Posted: Mon Jan 16, 2012 3:29 pm
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

Re: strange validation error

Posted: Mon Jan 16, 2012 3:44 pm
by cmdo
What do you mean?
I'm sorry but I do not understand :S

Re: strange validation error

Posted: Mon Jan 16, 2012 4:57 pm
by cmdo
I got it working
I changed it from picture to button with a background picture.
Thanks for the help.