how to prevent empty posted forms

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
rugbyfreak
Forum Newbie
Posts: 10
Joined: Sat May 16, 2009 12:41 pm

how to prevent empty posted forms

Post by rugbyfreak »

Code: Select all

 
if ($name != " " && $familyname != " " && $user != " " && $email != " " && $password != " ");
{
    echo "The if statement evaluated to true";
} else {
    echo "The if statement evaluated to false";
}
 
i'm trying to prevent empty posted forms by looking if their are filling something. but it doesn't works. nee help
and if someone knows how to say that you must fill in at least x characters and at least filling an "@" in the email form. thank you
Last edited by Benjamin on Sat May 16, 2009 1:04 pm, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: how to prevent empty posted forms

Post by Benjamin »

Please use

Code: Select all

tags when posting code and do not hijack other threads.  This question has been split from another users post.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: how to prevent empty posted forms

Post by Mark Baker »

Shouldn't be a semi-colon after the if()
" " isn't empty, it contains a single space
Consider using trim to clear any number of leading or trailing spaces from a value

Code: Select all

if (trim($name) != "" && trim($familyname) != "" && trim($user) != "" && trim($email) != "" && trim($password) != "")
or empty()
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: how to prevent empty posted forms

Post by Darhazer »

You are checking if the variables are not a single space... and if I don't fill the field, you will receive "", not " "

You can use strlen() function to ensure that the field have at least x characters, and ereg / preg to check the e-mail format. Additionally, as of PHP 5.2 you can check the e-mail with filter_var($variable, FILTER_VALIDATE_EMAIL)
rugbyfreak
Forum Newbie
Posts: 10
Joined: Sat May 16, 2009 12:41 pm

Re: how to prevent empty posted forms

Post by rugbyfreak »

thank you for the response:d:d
rugbyfreak
Forum Newbie
Posts: 10
Joined: Sat May 16, 2009 12:41 pm

Re: how to prevent empty posted forms

Post by rugbyfreak »

Code: Select all

 
  if (strlen($name) == 0 || strlen($familyname) == 0 || strlen($user) < 6 || strlen($password) < 8) || !eregi('@',$email) 
   {
  header('Location:fillfields.html');
  }
  else
  {
  echo: "blabla";
  }
doesn't work...
Last edited by Benjamin on Mon May 18, 2009 10:11 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: how to prevent empty posted forms

Post by Darhazer »

Give a parse error?

If so, you just missed one more ')' at the end of first line.
rugbyfreak
Forum Newbie
Posts: 10
Joined: Sat May 16, 2009 12:41 pm

Re: how to prevent empty posted forms

Post by rugbyfreak »

HTTP Error 404 Not Found

http://www.000webhost.com/

same with tha extr ')'
rugbyfreak
Forum Newbie
Posts: 10
Joined: Sat May 16, 2009 12:41 pm

Re: how to prevent empty posted forms

Post by rugbyfreak »

does anyone knows an answer?
TheBrandon
Forum Commoner
Posts: 87
Joined: Tue May 20, 2008 8:55 am

Re: how to prevent empty posted forms

Post by TheBrandon »

Are you sure you have a "fillfields.html" on your site?
rugbyfreak
Forum Newbie
Posts: 10
Joined: Sat May 16, 2009 12:41 pm

Re: how to prevent empty posted forms

Post by rugbyfreak »

yes...
Post Reply