Jump to content

PHP if else statement question


WebProgrammerNewb22

Recommended Posts

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:

 

//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(isempty($_POST['FirstNameTextBox']))
{
  // error
} elseif($_POST['LastNameTextBox']))
  //error
} elseif($_POST['PhoneNumberTextBox']))
  //error

 

and so on..

 

I am also unsure of the actual code for the error message.. 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!

 

Link to comment
Share on other sites

Can do something like this (substitute your variables of course)

 

if (isset($_POST['uname']) || isset($_POST['pword']))

{

//submitted, check values

if (empty($_POST['uname'])) { die ("Error: Enter a username"); }

if (empty($_POST['pword'])) { die ("Error: Enter a password"); }

}

Link to comment
Share on other sites

I have heard that ifempty is better to use, but I am not familiar with it.  Here is 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

 

Link to comment
Share on other sites

I have made two pages.  One called AddContact.php and once called ContactForm.html.  The html page contains the actual form and the action is set to AddContact.php where I have the code I pasted above.  I have uploaded the two files to see if anyone can help me figure out what is wrong with the code and/or how to get it to work.  It could possibly be my php configurations on my computer.  Any help is greatly appreciated!

 

[attachment deleted by admin]

Link to comment
Share on other sites

I hit f12 to preview and a page does come up with errors for my .php

 

^^^ That doesn't mean anything in this case because the .php page is the target of a form submission. Directly requesting it SHOULD produce undefined index errors.

 

You should however be testing if the form was submitted BEFORE referencing any of the form data. This is typically done by testing if the named submit button isset() and only executing the form processing code if it is.

 

I tried your form and form processing code and it does display the validation messages (at least the ones I tried) and if all the fields have values it does display the we will get back to you within 1 week ... message.

 

If you got a blank page, you are probably not doing this in an environment where the .php file is being processed through the php language engine or the actual code is not the same as the code you posted and the actual code contains a fatal parse error.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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