PHP if else

Coding for error checking

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1383 Views - Last Post: 24 October 2010 - 08:34 PM Rate Topic: -----

#1 WebProgrammerNewb22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-October 10

PHP if else

Posted 24 October 2010 - 06:20 PM

Hey guys.. I've been making a contact form and doing the error checking. Usually this is not a problem in any other language besides php. However, it is mandatory that I use php for this. I am very good at .NET and such, but have not used much php. I am using post method and trying to do error checking for null fields. I used java and got it all to work but am having trouble putting it into php. My variables are as follows:


<?php

//Define Variables
$FirstName = $_POST['FirstNameTextBox'];
$LastName = $_POST['LastNameTextBox'];
$PhoneNumber = $_POST['PhoneNumberTextBox'];
$EmailAddress = $_POST['EmailAddressTextBox'];
$Address = $_POST['AddressTextBox'];
$City = $_POST['CityTextBox'];
$State = $_POST['StateDropDownBox'];
$Zip = $_POST['ZipTextBox'];

?>


I was planning on using if else statements. For example:

if FirstNameTextBox == NULL

(coding for an error message that I am unsure of)

elseif LastNameTextBox == NULL

(coding for error message)

and so on...

Am I correct in using this approach?? As you can see all my code is pretty much vb language. I've been researching this for a while and can not figure out the most simple way to do this using these statements. Any help you professionals can lend to a beginner is appreciated greatly!

This post has been edited by CTphpnwb: 24 October 2010 - 06:37 PM
Reason for edit:: User failed to read the rules and use code tags!


Is This A Good Question/Topic? 0
  • +

Replies To: PHP if else

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2951
  • View blog
  • Posts: 10,172
  • Joined: 08-August 08

Re: PHP if else

Posted 24 October 2010 - 06:42 PM

First, use code tags!!!

Second, $_POST is a variable that is an array. Copying its values to other variables for no reason is pointless. Actually, it's worse than that because now you've moved them into other variables without sanitizing the values and it's only a matter of time before you forget that you need to do that. It's also possible that some of the values you expect haven't been posted, so there are more potential problems there!

if(isset($_POST['FirstNameTextBox']) && $_POST['FirstNameTextBox'] != "")
{
 // you've got a value here. What will you do with it?
} 

Was This Post Helpful? 0
  • +
  • -

#3 WebProgrammerNewb22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-October 10

Re: PHP if else

Posted 24 October 2010 - 06:52 PM

I probably should have explained my situation a bit better when I explained my code. I have an html page with a form. I need to use php if else statements to verify that certain boxes are filled in. From what I understand about POST, you use this method when you are declaring variables from text that is put into a field. The only other method I have been taught is GET, so please excuse my unfamiliarity. I was thinking something along the lines of this for my null field error checking..



if $FirstName = Null;

  echo (code for error message)

elseif $LastName = Null;

  echo (code for error messages)




and so on..

It seems like there should be a way to do it like this.. I know this would be the method I would use for VB.NET and such.. this php just isn't my thing!

PS- sorry i'm not so good with these code snippets!

This post has been edited by WebProgrammerNewb22: 24 October 2010 - 06:59 PM

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2951
  • View blog
  • Posts: 10,172
  • Joined: 08-August 08

Re: PHP if else

Posted 24 October 2010 - 07:01 PM

It doesn't matter whether you're using GET or POST. They're both arrays and should be treated the same way.
if(isempty($_POST['FirstNameTextBox']))
{
  // error
} elseif....

or
if(isset($_POST['FirstNameTextBox']) && $_POST['FirstNameTextBox'] != "")
{
 // you've got a value here. What will you do with it?
} 


The point is, you don't need to copy to another variable, and you shouldn't unless you have a good reason:

$firstname = mysql_real_escape_string($_POST['FirstNameTextBox']);

Was This Post Helpful? 0
  • +
  • -

#5 WebProgrammerNewb22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-October 10

Re: PHP if else

Posted 24 October 2010 - 07:09 PM

Okay. So I can us the following code for my null fields?? For the error message would I just use echo and then enter what I would like to come up on the following page.. or is there a short code for an error box to pop up saying "Required Fields Must Be Filled".. that would be my most ideal method.

if(isempty($_POST['FirstNameTextBox']))
{
  // error
} elseif($_POST['LastNameTextBox']))
  //error
} elseif($_POST['PhoneNumberTextBox']))
  //error



Really appreciate the help man I feel like a complete newb learning a new language right now..
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2951
  • View blog
  • Posts: 10,172
  • Joined: 08-August 08

Re: PHP if else

Posted 24 October 2010 - 07:31 PM

if(isempty($_POST['FirstNameTextBox']))
{
  // error
} elseif($_POST['LastNameTextBox']))
{  // <--- missing brace
  //error
} elseif($_POST['PhoneNumberTextBox']))
  //error


Yes, but you might want to create a function to do validation:
<?php
$my_keys = array("FirstNameTextBox", "LastNameTextBox", "PhoneNumberTextBox", "EmailAddressTextBox", "AddressTextBox", "CityTextBox", "StateDropDownBox", "ZipTextBox");
$myresponse = array("First Name", "Last Name" ,"Phone","Email", "Address","City","State","Zip");

$err = validate_input($my_keys);
for($i= 0; $i < count($err); $i++)
{
	echo $myresponse[$err[$i]]." is empty!";
}


function validate_input($keys)
{
	$errors = array();
	for($i= 0; $i < count($keys); $i++)
	{
		if(issempty($_POST[$keys[$i]]))
		{
			$errors[] = $i;
		}
	}
	return $errors;
}

?>

Then again, you might want to make something like that a method in a class. ;)

This post has been edited by CTphpnwb: 24 October 2010 - 07:34 PM
Reason for edit:: fixed echo statement.

Was This Post Helpful? 0
  • +
  • -

#7 WebProgrammerNewb22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-October 10

Re: PHP if else

Posted 24 October 2010 - 07:44 PM

awesome.. I really appreciate the help! I really would love to completely understand this code though so that I can get the most out of it. I am not just looking to get this done, but I am looking to completely understand it. Correct me if I am wrong and please explain other things to me:

In your first lines of your code you declared variables $my_keys and $myresponse.. is that a generic code or is that just a designated name you chose for your variable? Also, you then declared which items belong in your array. How is it that you made these responses line up? Does that question make sense? haha

I wouldn't want to use such advanced code for my class though I would probably be under the radar! I would definitely need to fully understand all of this to use it. Is there anyway you can explain some of it to me so that I can better understand. Like I said before I do have prior programming experience with VB.NET so it is not completely new, the arrays are tough though. Thanks again for the help
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2951
  • View blog
  • Posts: 10,172
  • Joined: 08-August 08

Re: PHP if else

Posted 24 October 2010 - 07:56 PM

I just chose them. I just wanted to show a generic method for validating. It's not complete, because it doesn't check that the values are valid (ie, a zipcode is a zipcode and a phone number is a phone number) but it expresses the idea.

As for arrays, php is very good with them, and I suggest you read up on their functions here.

By class, I meant an object:
<?php 
class rectangle {
	var $width = 5;
	var $length;
	var $color = "Blue";
	
	function area() {
		$this->thearea = $this->width * $this->length;
	}
	function perimeter() {
		$this->perimeter = 2*$this->width + 2*$this->length;
	}
	function output_stats(){
		echo "Length: ".$this->length." Width: ".$this->width."<br>Area: ".$this->thearea." Perimeter: ".$this->perimeter."<br><br>";
	}
}

for($i = 0; $i < 11; $i++)
{
	$box[] = new rectangle;
	$box[$i]->length = $i+2;
	$box[$i]->area();
	$box[$i]->perimeter();
	$box[$i]->output_stats();
}
?>

but I guess that's for another day.
Was This Post Helpful? 0
  • +
  • -

#9 WebProgrammerNewb22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-October 10

Re: PHP if else

Posted 24 October 2010 - 07:58 PM

Also, when I click on my submit button it takes me to a completely blank page. The way I have it set up is I have a ContactForm.html page with the actual form on it. Then the form action is set to AddContact.php. On the php page I have all the code that we have been discussing. This should be giving me different results correct?

wow I really wish I understood all of that. It just seems so sloppy compaired to good old VB.NET!!
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2951
  • View blog
  • Posts: 10,172
  • Joined: 08-August 08

Re: PHP if else

Posted 24 October 2010 - 08:10 PM

You're probably getting an error, and it's being logged. Look for a file called
error_log
or
php_error.log
or something similar.

You can find the path to that file in the php.ini file. Look in there for:
; Log errors to specified file.
Was This Post Helpful? 0
  • +
  • -

#11 WebProgrammerNewb22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-October 10

Re: PHP if else

Posted 24 October 2010 - 08:14 PM

Okay another effort..

I have tried just declaring a bunch of variables and doing it the long way.. it may look like a lot of work but it makes the most sense to me (a newb) However, when I click on submit, a blank page comes up with the extension AddContact.php. I explained my set up in the previous post. Is there a reason for this because I know it is not with my code. Errors or something should at least come up but not a blank page.

New Code:

//Define Variables
$FirstName = $_POST['FirstNameTextBox'];
$LastName = $_POST['LastNameTextBox'];
$PhoneNumber = $_POST['PhoneNumberTextBox'];
$EmailAddress = $_POST['EmailAddressTextBox'];
$Address = $_POST['AddressTextBox'];
$City = $_POST['CityTextBox'];
$State = $_POST['StateDropDownBox'];
$Zip = $_POST['ZipTextBox'];
$error1='*Please enter a First Name<br>';
$error2='*Please enter a Last Name<br>';
$error3='*Please enter a Phone Number<br>';
$error4='*Please choose a state<br>';
$error5='*Please enter a valid email address<br>';
$day2 = mktime(0,0,0,date("m"),date("d")+2,date("Y"));
$day3 = mktime(0,0,0,date("m"),date("d")+3,date("Y"));
$day7 = mktime(0,0,0,date("m"),date("d")+7,date("Y"));

if($FirstName=="") {echo $error1; exit;}
if($LastName=="") {echo $error2; exit;}
if($PhoneNumber=="") {echo $error3; exit;}
if($State=="") {echo $error4; exit;}
if($EmailAddress=="") {echo $error5; exit;}

if($State == "NY")
{
echo "$FirstName $LastName - we will get back to you within 2 days, ie 
before " .date("d M Y", $day2); exit;
} 
if($State == "NJ")
{
echo "$FirstName $LastName - we will get back to you within 3 days, ie 
before " .date("d M Y", $day3); exit;
} 
if($State == "Other")
{
echo "$FirstName $LastName - we will get back to you within 1 week, 
ie before " .date("d M Y", $day7);  exit;
} 




when I am in my program (expression web) I hit f12 to preview and a page does come up with errors for my .php page... the contactform.html page comes up correctly with my form on it, but again when I click submit on that page it goes to a blank page

The errors are as followed for my .php page:

Notice: Undefined index: FirstNameTextBox in C:\Users\Jonny P\Documents\My Web Sites\JMPMySite\AddContact.php on line 14

Notice: Undefined index: LastNameTextBox in C:\Users\Jonny P\Documents\My Web Sites\JMPMySite\AddContact.php on line 15

Notice: Undefined index: PhoneNumberTextBox in C:\Users\Jonny P\Documents\My Web Sites\JMPMySite\AddContact.php on line 16

Notice: Undefined index: EmailAddressTextBox in C:\Users\Jonny P\Documents\My Web Sites\JMPMySite\AddContact.php on line 17

Notice: Undefined index: AddressTextBox in C:\Users\Jonny P\Documents\My Web Sites\JMPMySite\AddContact.php on line 18

Notice: Undefined index: CityTextBox in C:\Users\Jonny P\Documents\My Web Sites\JMPMySite\AddContact.php on line 19

Notice: Undefined index: StateDropDownBox in C:\Users\Jonny P\Documents\My Web Sites\JMPMySite\AddContact.php on line 20

Notice: Undefined index: ZipTextBox in C:\Users\Jonny P\Documents\My Web Sites\JMPMySite\AddContact.php on line 21
*Please enter a First Name
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2951
  • View blog
  • Posts: 10,172
  • Joined: 08-August 08

Re: PHP if else

Posted 24 October 2010 - 08:15 PM

It seems to work for me. Do you have opening/closing php tags?
<?php
   // your code here.
?>

Yes, you do! You're getting those warnings because the post variables aren't set, yet you access them anyway. That's why you use isset() or isempty(), to avoid those problems.

This post has been edited by CTphpnwb: 24 October 2010 - 08:17 PM

Was This Post Helpful? 0
  • +
  • -

#13 WebProgrammerNewb22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-October 10

Re: PHP if else

Posted 24 October 2010 - 08:24 PM

Is the blue next to the variables that I used POST for not the text box that I am pulling the information out of??

How can I correct this error without using isser() or isempty()?? We did not yet learn that and I do not want to necessarily jump ahead unless I am completely clear.
Was This Post Helpful? 0
  • +
  • -

#14 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Re: PHP if else

Posted 24 October 2010 - 08:25 PM

Yes it is.
Was This Post Helpful? 0
  • +
  • -

#15 WebProgrammerNewb22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-October 10

Re: PHP if else

Posted 24 October 2010 - 08:26 PM

so then where is the undefined index?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2