Contact Form Validation Problem

Hi

I’m having a strange problem with the validation of a contact form. It looks like that

if (strlen(trim($_POST['message']) == 0)) is not working as it should.

Here is my code

[php]

<?php include "lib/configuration.php"; include "language/frontend/portuguese.php"; include "templates/frontend/header.tpl"; //If the form is submitted if(isset($_POST['submit'])) { $isError = False; //Check to make sure that the name field is not empty if (strlen(trim($_POST['contactname']) == 0)) { debug("is error in contactname: ", trim($_POST['contactname'])); debug("lengh: ", (strlen(trim($_POST['contactname'])))); $isError = true; } else { debug("no error in contactname: ", trim($_POST['contactname'])); debug("lengh: ", (strlen(trim($_POST['contactname'])))); $name = trim($_POST['contactname']); } //Check to make sure that the subject field is not empty if (strlen(trim($_POST['subject']) == 0)) { debug("is error in subject: ", trim($_POST['subject'])); debug("lengh: ", (strlen(trim($_POST['subject'])))); $isError = true; } else { debug("no error in subject: ", trim($_POST['subject'])); debug("lengh: ", (strlen(trim($_POST['subject'])))); $subject = trim($_POST['subject']); } //Check to make sure sure that a valid email address is submitted if (strlen(trim($_POST['email']) == 0)) { debug("is error in email: ", trim($_POST['email'])); debug("lengh: ", (strlen(trim($_POST['email'])))); $isError = true; } else if (!filter_var(trim($_POST['email']), FILTER_VALIDATE_EMAIL)) { $isError = true; } else { debug("no error in email: ", trim($_POST['email'])); debug("lengh: ", (strlen(trim($_POST['email'])))); $emailFrom = trim($_POST['email']); } //Check to make sure comments were entered if (strlen(trim($_POST['message']) == 0)) { debug("is error in message: ", trim($_POST['message'])); debug("lengh: ", (strlen(trim($_POST['message'])))); $isError = true; } else { if (function_exists('stripslashes')) { $comments = stripslashes(trim($_POST['message'])); } else { debug("no error in message: ", trim($_POST['message'])); debug("lengh: ", (strlen(trim($_POST['message'])))); $comments = trim($_POST['message']); } } //If there is no error, send the email if ($isError = false) { $emailTo = $adminemail; $body = "Name: $name \n\nEmail: $emailFrom \n\nSubject: $subject \n\nComments:\n $comments"; $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $emailFrom; mail($emailTo, $subject, $body, $headers); echo ""; echo "
" . $contactinfo['sent'] . "
"; } else { echo ""; echo "
" . $contacterror['sent'] . "
"; } } else { include "templates/frontend/loginspace.tpl"; include "templates/frontend/registerlogin.tpl"; include "templates/frontend/searchspace.tpl"; include "templates/frontend/search.tpl"; include "templates/frontend/hmenuspace.tpl"; include "templates/frontend/menubar.tpl"; include "templates/frontend/leftspace.tpl"; include "templates/frontend/leftside.tpl"; include "templates/frontend/contentspace.tpl"; include "templates/frontend/contact.tpl"; include "templates/frontend/rightspace.tpl"; if (isset($_SESSION['logedin'])) { include "templates/frontend/myaccount.tpl"; } include "templates/frontend/rightside.tpl"; include "templates/frontend/footerspace.tpl"; include "templates/frontend/footerdiv.tpl"; include "templates/frontend/footer.tpl"; } ?>

[/php]

The log of the debugger I setup is this

[php]
2013-06-22 14:06:49 - is error in contactname: : Virginio Reis
2013-06-22 14:06:49 - lengh: : 13
2013-06-22 14:06:49 - is error in subject: : Teste
2013-06-22 14:06:49 - lengh: : 5
2013-06-22 14:06:49 - is error in email: : [email protected]
2013-06-22 14:06:49 - lengh: : 16
2013-06-22 14:06:49 - is error in message: : mensagem de teste
2013-06-22 14:06:49 - lengh: : 17
[/php]

What is wrong with this code? Can someone help me_

Thank You

I’ve done this before too, the ) bracket is in the wrong place. The == 0 needs to be outside the strlen()
[php]strlen(trim($_POST[‘contactname’])) == 0[/php]

Thank You. did not see that

A much simpler way of detecting 0 characters would be to do if(empty($_POST[‘message’])) {}. And you only need to put that debug code in the first part so you know if something went wrong, don’t need it if its working.

I found many ways of checkin if a string is empty. this is just one of them. Thank You anyway
the debug is just that:debug. it will go way when it is working.

Its giving you false information though. This is what it should be
[php]if(strlen(trim($_POST[‘contactname’]) == 0)) {
debug("is error in contactname: ", trim($_POST[‘contactname’]));
debug("lengh: ", (strlen(trim($_POST[‘contactname’]))));
$isError = true;
} else {
$name = trim($_POST[‘contactname’]);
}[/php]
Saves the eyes, space and gives you the proper debug information

It is solved. Thank You anyway

going to take you double the amount of time to figure out what’s going on, but whatever.

Sponsor our Newsletter | Privacy Policy | Terms of Service