conditional statement not working

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
JustPlainJef
Forum Commoner
Posts: 42
Joined: Tue Jan 04, 2011 5:04 am
Location: McHenry, IL

conditional statement not working

Post by JustPlainJef »

Morning all.

I have a conditional test that's not cooperating. If I test either half of the equation, it works. But when I put the OR between the two, it doesn't work right. Full code is below, here's my test.
IF (($HomePhone != ' ') OR (preg_match("/^[0-9]{10}$/", $HomePhone) != '1)')
Since we aren't going to require a home phone #, I have to check to see if it's 10 digits, or blank. I have a similar test to check for positive match, and that one works. Here's the odd part. If I put in a bad phone #, it says "Bad phone." If I put in a bad zip code OR a bad state code, both will echo "You have a bad phone #" on top of "you have a bad state / zip." So if I have good data, then make the state code bad, it tells me the phone # is bad. If I test for blank, it works. If I test for 10 digits, it works. If I test for blank OR ten digits, fail.

The test in question is under the line of ###.

Code: Select all

<?php 

	require_once 'login.php';

	#connect to the database
	#$cxn=mysqli_connect($db_hostname, $db_username, $db_password, $db_database);
	#If (!$cxn) die ("Error, could not connect to the database<BR>");

	require_once 'header.php';

	#First visit to the page, just display the form.
	IF (EMPTY($_POST))
		 {
		 ECHO "<h2>Hello and welcome!</h2>"
		 	."We are going to start with the household information.  Don't worry, it won't be too painfull.<br /><br /><br />"
			."<form action='household.php' method='POST'>"

			."Street Address:  <input type = 'text' name = 'Street1'><br /><br />"
			."Street Address 2:  <input type = 'text' name = 'Street2'><br /><br />"
			."City:  <input type = 'text' name = 'City' value = 'McHenry'><br /><br />"
			."State:  <input type = 'text' name = 'State' value = 'IL'><br /><br />"
			."Zip Code:  <input type = 'number' name = 'ZipCode' value = '60050'><br /><br />"
			."Home Phone:  <input type = 'tel' name = 'HomePhone'><br /><br />"
			."Emergency Contact:  <input type = 'text' name = 'EmergencyName'><br /><br />"
			."Emergency Phone #:  <input type = 'tel' name = 'EmergencyNumber'><br /><br />"
			
			."<input type='submit' value='Next step'>"
			."</form>";
		 }
	#If $_POST is filled, then check the data
	ELSE
		 {	
		#Pull the data from $_POST and put them into variables
		 $Street1 = $_POST['Street1'];
		 $Street2 = $_POST['Street2'];
		 $City = $_POST['City'];
		 $State = $_POST['State'];
		 $ZipCode = $_POST['ZipCode'];
		 $HomePhone = $_POST['HomePhone'];
		 $EmergencyName = $_POST['EmergencyName'];
		 $EmergencyNumber = $_POST['EmergencyNumber'];
		#Strip non-numbers from phone # and zip code 
		$HomePhone = preg_replace("/(\D+)/", "", $HomePhone);
		 $ZipCode = preg_replace("/(\D+)/", "", $ZipCode);
		 ECHO "Phone: $HomePhone<br />";
	#These are in for testing because stuff isn't working......	
	#$pp = preg_match("/^[0-9]{10}$/", $HomePhone);
	#echo "PP is $pp.<br />";

		 #Missing required data, re-post form with required fields in red.
		 IF (empty($Street1) || empty($City) || empty($State) || empty($ZipCode))
		 	{
		 	ECHO "Please verify required fields are filled in!"
		 	."<form action='household.php' method='POST'>"
		 	."<b><font color='red'>Street Address:</font></b>  <input type = 'text' name = 'Street1' value = '$Street1'><br /><br />"
		 	."Street Address 2:  <input type = 'text' name = 'Street2' value = '$Street2'><br /><br />"
		 	."<b><font color='red'>City:</font></b>  <input type = 'text' name = 'City' value = '$City'><br /><br />"
		 	."<b><font color='red'>State:</font></b>  <input type = 'text' name = 'State' value = '$State'><br /><br />"
		 	."<b><font color='red'>Zip Code:</font></b>  <input type = 'number' name = 'ZipCode' value = '$ZipCode'><br /><br />"
		 	."Home Phone:  <input type = 'tel' name = 'HomePhone' value = '$HomePhone'><br /><br />"
		 	."Emergency Contact:  <input type = 'text' name = 'EmergencyName' value = '$EmergencyName'><br /><br />"
		 	."Emergency Phone #:  <input type = 'tel' name = 'EmergencyNumber' value = '$EmergencyNumber'><br /><br />"

		 	."<input type='submit' value='Log in'>"
		 	."</form><br /><br />";
		 	}
	
		#Good data
		ELSE IF ((preg_match("/^[0-9]{10}$/", $HomePhone) || ($HomePhone == '')) AND preg_match("/^[0-9]{5}$/", $ZipCode) AND preg_match("/^[A-Z]{2}$/i", $State))
	   		 {
			 ECHO "Good data.<br />";
			 }

#############################################################################################

		#Bad data	 
		ELSE
		{
			#Bad phone #
			IF (($HomePhone != '') OR (preg_match("/^[0-9]{10}$/", $HomePhone) != '1'))
			 {
			 ECHO "Please check your home phone format.  It needs to be 10 digits.<br />"
			."Acceptable:  (815)555-5555 or 815-555-5555 or 8155555555.<br />";
		  	 }
			#Bad zip code
			IF (!preg_match("/^[0-9]{5}$/", $ZipCode))
		 	{
		 	ECHO "Please verify your zip code is 5 digits.";
			}
		 	#Bad state code
			IF (!preg_match("/^[A-Z]{2}$/i", $State))
			{
			ECHO "Please verify your state code is 2 letters.";
			}
		 	#repost the form
			ECHO "<form action='household.php' method='POST'>"
		 	."Street Address:  <input type = 'text' name = 'Street1' value = '$Street1'><br /><br />"
		 	."Street Address 2:  <input type = 'text' name = 'Street2' value = '$Street2'><br /><br />"
		 	."City:  <input type = 'text' name = 'City' value = '$City'><br /><br />"
		 	."State:  <input type = 'text' name = 'State' value = '$State'><br /><br />"
		 	."Zip Code:  <input type = 'number' name = 'ZipCode' value = '$ZipCode'><br /><br />"
		 	."Home Phone:  <input type = 'tel' name = 'HomePhone' value = '$HomePhone'><br /><br />"
		 	."Emergency Contact:  <input type = 'text' name = 'EmergencyName' value = '$EmergencyName'><br /><br />"
		 	."Emergency Phone #:  <input type = 'tel' name = 'EmergencyNumber' value = '$EmergencyNumber'><br /><br />"

			."<input type='submit' value='Log in'>"
		 	."</form>";
		 	}
		 }
		 
?>
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: conditional statement not working

Post by Darhazer »

Code: Select all

 IF (($HomePhone != '') OR (preg_match("/^[0-9]{10}$/", $HomePhone) != '1'))
is the same as

Code: Select all

IF (( ($HomePhone != '') OR (preg_match("/^[0-9]{10}$/", $HomePhone) ) != '1')
and I assume you mean:

Code: Select all

IF (($HomePhone != '') OR (preg_match("/^[0-9]{10}$/", $HomePhone)  != 1))
And in my optinion it would be correct to check:

Code: Select all

IF (($HomePhone != '') AND (preg_match("/^[0-9]{10}$/", $HomePhone) != 1))
meaning that you'll check for 10 digits only if HomePhone is not empty
JustPlainJef
Forum Commoner
Posts: 42
Joined: Tue Jan 04, 2011 5:04 am
Location: McHenry, IL

Re: conditional statement not working

Post by JustPlainJef »

Changing the OR to an AND worked! Thanks for your help!

I'm not quite sure why it would be AND, but as long as it works, I'm happy...
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: conditional statement not working

Post by Darhazer »

because with an 'OR' you do allow any $HomePhone which is not '' (remember, if any of the statements is true, or returns true)
And you are not allowing empty $HomePhone, because both of the statements will fail with empty phone number
Post Reply