Jump to content

[SOLVED] SImple Form, IF Statement Help


SilentQ-noob-

Recommended Posts

I've made a simple form, and want to make an error message for fields not being filled out - this is what I have

 

$len = 1;
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
print "<br />";

if(strlen($name) < $len) {
echo "The Name field has not been filled out";
print "<br />";

if(strlen($lastname) < $len) 
echo "The Last Name field has not been filled out";
print "<br />";

if(strlen($email) < $len) 
echo "The E-mail field has not been filled out";
print "<br />";

}
else {
echo " thanks for filling out our form" ;

 

My problem is, is that I dont know how to make the else part not appear. For example, if I fill in only one field, it tells me that the others need to be filled out, but also states "thanks for filling out our form". Any Help/Advice appreciated.

Link to comment
Share on other sites

Your problem lies with the braces. Each if statement should have separate braces - the way you have it, the last name and email error messages will only print if the name field has not been filled out. Instead of what you have, you need this:

 

if(strlen($name) < $len) {
echo "The Name field has not been filled out";
print "<br />";
}

if(strlen($lastname) < $len) {
echo "The Last Name field has not been filled out";
print "<br />";
}

if(strlen($email) < $len) {
echo "The E-mail field has not been filled out";
print "<br />";
}

 

Also, There are a few things you can do to improve this script.

 

1) Use the empty() function to see if a string is empty. Read up on it as well. http://us.php.net/empty

 

if (empty($name))
     //Do whatever

 

2) Create a variable that will receive the desired strings and concatenate each error to the variable. Then check to see if the variable is empty. If it is, then there are no errors and you can echo "Thanks for filling out the form.

 


$errors = "";
if (empty($name))
     $errors .= "The name field has not been filled out <br>";
if (empty($lastname))
     $errors .= "The last name field has not been filled out <br>";
if (empty($email))
      $errors .= "The email field has not been filled out <br>";

if (empty($errors))
     echo "Thanks!"
else
     echo $errors;

Link to comment
Share on other sites

This way it has to past each step to get to the next check.

$len = 1;
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
print "<br />";

if(empty($name)) {echo "The Name field has not been filled out<br>";} else {
if(empty($lastname))  {echo "The Last Name field has not been filled out<br>";} else {
	if(empty($email)) {echo "The E-mail field has not been filled out<br>";} else {
		echo " thanks for filling out our form" ;
	}
}
}
[\code]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.