3 Replies - 286 Views - Last Post: 18 June 2011 - 10:26 AM Rate Topic: -----

#1 RyanRobinson  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 225
  • Joined: 31-March 10

FALSE if statement doesn't work

Posted 18 June 2011 - 09:29 AM

Hi guys,

I have created a form that checks whether the name and email forms have been filled in.

if(isset($_POST['checker']))
	{
		if(isset($_POST['nameStr']))
		{
			$name = cleaninput($_POST['nameStr']);
		}else {
			echo "<p>You failed to enter your name.</p>";
		}
		
		if(isset($_POST['emailStr']))
		{
			$email = cleaninput($_POST['emailStr']);
		}else {
			echo "<p>You failed to enter your email.</p>";
		}
		
		if($name && $email == TRUE)
		{
			echo insertdata($name, $email);
		}else {
			echo "<p>There was an error. Please enter your details again</p>";	
		}
	}



However, the "else" code that tests whether it is set or not doesn't execute. What happens is, when I submit the empty fields the last "else" runs when I am testing that $name and $email == TRUE. Why does the the error trapping get missed on the testing for the individual name and email?

Is This A Good Question/Topic? 0
  • +

Replies To: FALSE if statement doesn't work

#2 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 112
  • View blog
  • Posts: 454
  • Joined: 27-June 10

Re: FALSE if statement doesn't work

Posted 18 June 2011 - 09:56 AM

I believe it's because you use isset, when you should be using !empty().

http://www.htmlcente...d-isset-in-php/

if(isset($_POST['checker']))
	{
		if(!empty($_POST['nameStr']))
		{
			$name = cleaninput($_POST['nameStr']);
		}else {
			echo "<p>You failed to enter your name.</p>";
		}
		
		if(!empty($_POST['emailStr']))
		{
			$email = cleaninput($_POST['emailStr']);
		}else {
			echo "<p>You failed to enter your email.</p>";
		}
		
		if(isset($name && $email))
		{
			echo insertdata($name, $email);
		}else {
			echo "<p>There was an error. Please enter your details again</p>";	
		}
	}




Was This Post Helpful? 2
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: FALSE if statement doesn't work

Posted 18 June 2011 - 10:01 AM

The more fields you have, the longer your code will be. That's not going to be something you want to deal with six months from now when you need to make changes.
Do something like this instead:
$expected_fields = array("name" => "Name", "address" => "Address", "phone" => "Phone");
$error = "";
$received_fields = array();
foreach($expected_fields as $field_name => $key_name) {
	if(isset($_POST[$field_name]) && !empty($_POST[$field_name])) {
		$received_fields[$field_name] = mysqli_real_escape_string($_POST[$field_name]);
	} else {
		$error .= "The field: ".$key_name." is empty.<br />";
	}
}
if($error == "") {
	process_fields($received_fields);
} else {
	echo $error;
}

All you have to do is edit the $expected_fields array and write the process_fields function. ;)

This post has been edited by CTphpnwb: 18 June 2011 - 10:28 AM
Reason for edit:: fixed a but in the first if statement!

Was This Post Helpful? 4
  • +
  • -

#4 RyanRobinson  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 225
  • Joined: 31-March 10

Re: FALSE if statement doesn't work

Posted 18 June 2011 - 10:26 AM

Thanks guys.

I was just using the wrong function.

Thanks for the code for the more systematic error checking. I shall apply it when I will be building bigger forms in the future.

I also had to change your syntax on the last If statement. The variables had to be comma separated instead of using &&.

if(isset($name, $email))
		{
			echo insertdata($name, $email);
		}else {
			echo "<p class=\"error\">There was an error. Please enter your details again</p>";	
		}


This post has been edited by RyanRobinson: 18 June 2011 - 10:29 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1