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
https://forums.phpfreaks.com/topic/60841-solved-simple-form-if-statement-help/
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;

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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.