Jump to content

Recommended Posts

Hey guys.. I'm new to the forum and have a quick question about some coding i've been doing for my website.  A couple index errors are coming up when I run my code, but I believe it all should be working fine.  I am going to paste the code, but also upload the files so that you can understand the problem better.  ANY help is greatly appreciated.  I am currently making a contact form with validation.  I know that using ifempty() is probably the best way, but I am unclear as to how to use it.  I have two files, an html containing my form, and a php file containing the following code:

 



//Define Variables
$FirstName = $_GET['FirstNameTextBox'];
$LastName = $_GET['LastNameTextBox'];
$PhoneNumber = $_GET['PhoneNumberTextBox'];
$EmailAddress = $_GET['EmailAddressTextBox'];
$Address = $_GET['AddressTextBox'];
$City = $_GET['CityTextBox'];
$State = $_GET['StateDropDownBox'];
$Zip = $_GET['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;
} 


 

 

The following errors come up:

 

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

 

Again, ANY help is greatly appreciated.. it is for a class, but I have honestly exhasted all my sources to figure out what is wrong.  Are my codes correct and all there?  Thanks for the help!

 

-WPN

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/216810-data-validation-error-question/
Share on other sites

You have PHP in strict mode, which is why you are getting those errors

 

if(isset( $_GET['FirstNameTextBox']))
     $FirstName = $_GET['FirstNameTextBox'];
if(isset( $_GET['LastNameTextBox']))
     $LastName = $_GET['LastNameTextBox'];
if(isset( $_GET['PhoneNumberTextBox']))
     $PhoneNumber = $_GET['PhoneNumberTextBox'];
if(isset( $_GET['EmailAddressTextBox']))
     $EmailAddress = $_GET['EmailAddressTextBox'];
if(isset( $_GET['AddressTextBox']))
     $Address = $_GET['AddressTextBox'];

 

or place this at the beginning of the file:

ini_set('display_errors', '0');

I am not 100% what you mean, but if I am correct you can do this:

if(isset( $_GET['FirstNameTextBox']))
     $FirstName = $_GET['FirstNameTextBox'];
else
     $FirstName = '';
if(isset( $_GET['LastNameTextBox']))
     $LastName = $_GET['LastNameTextBox'];
else
     $LastName = ''; 
if(isset( $_GET['PhoneNumberTextBox']))
     $PhoneNumber = $_GET['PhoneNumberTextBox'];
else
     $PhoneNumber =  '';
if(isset( $_GET['EmailAddressTextBox']))
     $EmailAddress = $_GET['EmailAddressTextBox'];
else
     $EmailAddress =  '';
if(isset( $_GET['AddressTextBox']))
     $Address = $_GET['AddressTextBox'];
else
     $Address =  '';

No need to get complicated here. Your current code simply uses this validation to check if the GET value was not set or was a null string

if($FirstName=="") {echo $error1; exit;}

 

So, just change the way that you set the value. Change this

$FirstName = $_GET['FirstNameTextBox'];

 

To this

$FirstName = (isset($_GET['FirstNameTextBox'])) ? $_GET['FirstNameTextBox' : '';

 

Then all your existing code will work as designed. Do the same for all the other values you set from the GET values.

I'm sorry for not being clear as to what I have to do.. I need error checking that checks for data type, size, and basic format.  Required fields are First Name, Last Name, Phone, State, and email.  So I would replace the code that you mentioned with the required fields only correct?

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.