Jump to content

If else statements...


ZeroShade

Recommended Posts

How can I set my code up so that if the user does not put in anything for last name but there is something in first name... it will still say please go back to put your information in and vice versa. If both are put in and everything is fine... then the order is complete statement is executed? I can only figure out half of it, but the rest I can't get it to work.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">	<head>		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />		<title>CompuDeal Order</title>		<link rel="stylesheet" href="styles.css" type="text/css">	</head>	<body>			<fieldset div id="submit">			<?php				$fName = $_POST['fName'];				$lName = $_POST['lName'];				$address = $_POST['address'];							$city = $_POST['city'];							$province = $_POST['province'];							$country = $_POST['country'];							$postal = $_POST['postal'];							$number = $_POST['number'];							$computer = $_POST['computer'];							$proc = $_POST['proc'];							$memory = $_POST['memory'];							$space = $_POST['space'];							$color = $_POST['color'];							$quantityNumber = $_POST['quantityNumber'];							$ship = $_POST['ship'];							$pay = $_POST['pay'];								if(ctype_alpha($fName) == false)					info();				else					echo "<p />You have missed a piece of information, please go back to fill it in.";				function info()				{					echo "<h3>Thank you for Ordering from CompuDeal! Your order has been processed.</h3>";					echo "<h3>Below is a summary of your order:</h3>";				}			?>				</fieldset>	</body></html>

Link to comment
Share on other sites

I can think of 2 ways to do this, one way being much easier in my opinion. First, you could have it validate the information before it sends the form.

if ((isset($fName)) && (isset($lName)) {This is where you would put the code that you are using to complete submitting the form.}else {Don't send the form.}

In the else bracket you could check for which fields have not been filled in, set some variables and put a message at the top of the page, set some text to red, or add whatever you would like to alert them to the fields they didn't fill out.

Link to comment
Share on other sites

That code won't tell if they filled anything out, it will only tell if they submitted the form. Since you are using isset, the variables will still be set, they just might be empty. I normally just have a variable that keeps track of errors.

$errors = "";if ($fname == "")  $errors .= "Please fill in a first name<br>";if ($lname == "")  $errors .= "Please fill in a last name<br>";...if ($errors == ""){  //no errors}else  echo $errors;

Link to comment
Share on other sites

That code won't tell if they filled anything out, it will only tell if they submitted the form. Since you are using isset, the variables will still be set, they just might be empty. I normally just have a variable that keeps track of errors.
$errors = "";if ($fname == "")  $errors .= "Please fill in a first name<br>";if ($lname == "")  $errors .= "Please fill in a last name<br>";...if ($errors == ""){  //no errors}else  echo $errors;

Thats ingenious! :)... things are always this simple... I don't know why I don't clue in?
Link to comment
Share on other sites

Seems easy... but its not working... any reasons?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">		<head>				<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />				<title>CompuDeal Order</title>				<link rel="stylesheet" href="styles.css" type="text/css">		</head>		<body>				<fieldset div id="submit">						<?php								$fName = $_POST['fName'];								$lName = $_POST['lName'];								$address = $_POST['address'];								$city = $_POST['city'];								$province = $_POST['province'];								$country = $_POST['country'];								$postal = $_POST['postal'];								$number = $_POST['number'];								$computer = $_POST['computer'];								$proc = $_POST['proc'];								$memory = $_POST['memory'];								$space = $_POST['space'];								$color = $_POST['color'];								$quantityNumber = $_POST['quantityNumber'];								$ship = $_POST['ship'];								$pay = $_POST['pay'];								$error = "";								if(ctype_alpha($fName) == $error)										echo "<p />Please fill in your first name.<br />";								if($error == "");					  echo $error;				else					  info();								function info()								{										echo "<h3>Thank you for Ordering from CompuDeal! Your order has been processed.</h3>";										echo "<h3>Below is a summary of your order:</h3>";								}						?>				</fieldset>		</body></html>

Link to comment
Share on other sites

if(ctype_alpha($fName) == $error)  echo "<p />Please fill in your first name.<br />";

What are you doing there? Look at the code that I wrote, look at your code again, and see if you can figure out what you need to change.

Link to comment
Share on other sites

This is insane... I still can't get it to work... and tired or not... I'm pretty sure its looking the same as to what you have. This is what I have but it's working funny...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">		<head>				<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />				<title>CompuDeal Order</title>				<link rel="stylesheet" href="styles.css" type="text/css">		</head>		<body>				<fieldset div id="submit">						<?php								$fName = $_POST['fName'];								$lName = $_POST['lName'];								$address = $_POST['address'];								$city = $_POST['city'];								$province = $_POST['province'];								$country = $_POST['country'];								$postal = $_POST['postal'];								$number = $_POST['number'];								$computer = $_POST['computer'];								$proc = $_POST['proc'];								$memory = $_POST['memory'];								$space = $_POST['space'];								$color = $_POST['color'];								$quantityNumber = $_POST['quantityNumber'];								$ship = $_POST['ship'];								$pay = $_POST['pay'];								$error = "";								if(ctype_alpha($fName) == "")										$error .= "<p />Please fill in your first name.<br />";								if($error == "")										info();								else										echo $error;								function info()								{										echo "<h3>Thank you for Ordering from CompuDeal! Your order has been processed.</h3>";										echo "<h3>Below is a summary of your order:</h3>";								}						?>				</fieldset>		</body></html>

If fName == "" then the error is executed... and if the fName is anything else like == " " || even "ladida" then no matter what the other statement executes. I'm frustrated.

Link to comment
Share on other sites

Hm, Not sure I followed, but:if fName is empty, then you get an error, if it's not empty, you don't get the error! right?Isn't that what you want it to do?Besides; you are testing the return of ctype_alpha() with an empty string, when it returns a boolean:

if (ctype_alpha($fName) == "")	  $error .= "<p />Please fill in your first name.<br />";if ($error == "")	info();else	echo $error;

Change it to

if (!ctype_alpha($fName))	  $error .= "<p />Please fill in your first name.<br />";

can't see any problems other than thatGood Luck and Don't Panic!

Link to comment
Share on other sites

Don't use ctype_alpha at all, just compare the string directly with an empty string:if ($fName == "")Like you said above, that statement is only true if $fName is an empty string. If $fName is a space, that's not the same thing as an empty string. If you want to trim spaces off the ends of the input, you need to do that yourself.$fName = trim($_POST['fName']);

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...